There is an existing function dev.copy2pdf()
, which does just what it says it will. There is also a function dev.print()
which looks like it would make copying to a "*.png"
file easy, but is in my experience pretty hinky.
The following two functions work well and have the added advantages of being uniformly named, and of taking all of their arguments in the same order as the familiar and related pdf()
and png()
functions:
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)