I would like to have a plot in both pdf and png formats:
pdf("test.pdf")
plot(sin, -pi, 2*pi)
dev.off()
png("test.png")
plot(sin, -pi, 2*pi)
dev.off()
But, I am searching for a trick (preferably, not by loading a new package) in which plot function only be called once:
#no plot in pdf!
pdf("test1.pdf"); png("test1.png")
plot(sin, -pi, 2*pi)
dev.off(); dev.off()
Any suggestion would be appreciated.
You can use
dev.copy()
for your purpose. For instance:You take note of the
pdf
device throughdev.cur
and then copy the plot from thepng
device to thepdf
one.Not sure if this approach has any advantages over @nicolas answer and it technically does not answer your question, but it certainly demonstrates the perks of R's non-standard evaluation and solves your problem in a clean way:
In englisch: Write your own function that takes the unevaluated plot command as argument and simply evaluates it [=plots] once for each device.