[R] .SavedPlots and replayPlot

Jean-Luc Dupouey dupouey at nancy.inra.fr
Thu Mar 5 00:42:32 CET 2015

I would like to redraw a plot from the .SavedPlots object (list), using 
R code (not arrows on the keyboard).

 > windows(record=TRUE)
 > x=runif(100)
 > plot(x,col="blue")
 > hist(x,col="red")
 > plot(x,col="green")

 > #When I try to replay any of the recorded plots (here, the second 
one), I get the following error message:

 > .SavedPlots[2]
Error in replayPlot(x) : loading snapshot from a different session

But I am in the same session! A quick look at function replayPlot shows 
that it is because the plots recorded in .SavedPlot do not have any pid 
value, whereas they should have one, equal to the current session pid.

Thus, assignation of the current session pid to the pid attribute of the 
recorded plots works:

 > for (i in 1:.SavedPlots[[2]]) 
 > #correctly gives the second plot:
 > .SavedPlots[2]

Is it the right way to replay a plot in an R script, using .SavedPlots? 
Is there a simpler or better way?

I would prefer not to use recordPlot, which works, but requires an 
additional line of code and a new variable for each created plot:

 > plot(x,col="blue")
 > plot1=recordPlot()
 > hist(x,col="red")
 > plot2=recordPlot()
 > plot(x,col="green")
 > plot3=recordPlot()
 > replayPlot(plot2)

I use R version 3.1.2.

Thanks in advance,

Jean-Luc Dupouey

Forest Ecology and Ecophysiology Unit
F-54280 Champenoux

More information about the R-help mailing list