[R] function in order to plot the same graph to postscript and pdf

Ronny Klein ronny.klein at wiwi.uni-halle.de
Thu Mar 10 09:32:55 CET 2005


> I guess you would want eval(MYPLOT) there?
>
> But the function above works if you say
>   plot.both(myplot = expression(plot(x)), filename = "foo")
> and that is what I said above: set myplot = expression(plot(x)).
> Z

My syntax in the example was a little bit messed, sorry for that.

However Gabor Grothendieck's reply has brought me to the solution. The 
following function does the job right:

print.both <- function(myplot, filename){
     pdf(file=paste(filename, ".pdf", sep=""))
     postscript(file=paste(filename, ".eps", sep=""))
    dev.control(displaylist="enable")
    myplot
    dev.copy(which=dev.next())
    graphics.off()
}

The dev.control(displaylist="enable") is necessary here because only then R 
does record the plot and dev.copy can be used.

So, thank you all for your help.

Ronny




More information about the R-help mailing list