Create PDF and PNG at the same time

Is there a way in R to create png and pdf at the same time without having to run the same code twice? I would like for both the raster and vector versions of some numbers to be created without the need for external conversion and without the ability to re-run the code. dev.copy2pdfIt sounds like I need, but it seems that it only works with interactive devices.

Here is an example of what I am doing:

pdf("temp.pdf")
plot(1:10)
#more drawing here
dev.off()

png("temp.png")
plot(1:10)
#more drawing here
dev.off()

Who I want to shorten:

start()
plot(1:10)
#more drawing here
saveToPDF()
saveToPNG()
+4
source share
1 answer

There is an existing function dev.copy2pdf()that does what it says. There is also a function dev.print()that is similar in that it easily copies a file "*.png", but in my opinion is pretty Hanchen.

, pdf() png():

saveToPDF <- function(...) {
    d = dev.copy(pdf,...)
    dev.off(d)
}

saveToPNG <- function(...) {
    d = dev.copy(png,...)
    dev.off(d)
}

## Try them out
plot(rnorm(99),  col="red")
saveToPDF("my.pdf", height=4,width=7)
saveToPNG("my.png", height=600, width=400)
+4

Source: https://habr.com/ru/post/1540606/


All Articles