[R] Save generic plot to file (before rendering to device)

Uwe Ligges ligges at statistik.tu-dortmund.de
Sat Jul 16 19:18:13 CEST 2011



On 16.07.2011 13:42, Jeroen Ooms wrote:
>>
>> That warning is necessarily rather pessimistic.  We haven't changed the
>> format for several years.  But we might, without notice.
>>
>
> Thank you, this is very helpful. One final question regarding this method:
> suppose a function prints multiple plots, i.e. multiple pages to a PDF. Is
> it possible to record all of these plots at once? The code below only
> records the final plot. I would like to record all of them, without
> modifying myfn:

You cannot, since this takes a snapshot from the current device. You 
will have to recordPlot() after each plot, actually.

Uwe Ligges




>
> myfn<- function(mylm){
>    plot(mylm, ask=F)
> }
>
> mylm<- lm(dist~speed, data=cars);
>
> pdf(tempfile())
> dev.control(displaylist="enable")
> myfn(mylm);
> myplots<- recordPlot()
> dev.off()
>
> save(myplot, file="myplot.RData")
>
>
>
>
>
>
>>
>>
>>
>>> Another approach (or the start to one that you could build on) is the
>>> plot2script function in the TeachingDemos package (which uses recordPlot
>>> internally).
>>>
>>> But it is probably best to save the resulting data from the long process,
>>> that could then be quickly plotted as others have mentioned.
>>>
>>> --
>>> Gregory (Greg) L. Snow Ph.D.
>>> Statistical Data Center
>>> Intermountain Healthcare
>>> greg.snow at imail.org
>>> 801.408.8111
>>>
>>>
>>>   -----Original Message-----
>>>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>>>> project.org] On Behalf Of Jeroen Ooms
>>>> Sent: Monday, July 11, 2011 12:22 PM
>>>> To: David Winsemius
>>>> Cc: r-help at r-project.org
>>>> Subject: Re: [R] Save generic plot to file (before rendering to device)
>>>>
>>>>
>>>>> You can also save a data (or function) object to an .Rdata file with
>>>>> save(objname, file="filename.Rdata")  and your colleagues could then
>>>>> load("filename.Rdata") in R.
>>>>>
>>>>>
>>>> Thanks for the responses. I found an old post by Gabor Grothendieck
>>>> that
>>>> shows what I want. Basically the trick is to save the displaylist to an
>>>> object:
>>>>
>>>> dev.control(displaylist="**enable") # enable display list
>>>> plot(1:10)
>>>> myplot<- recordPlot() # load displaylist into variable
>>>>
>>>> You can now store myplot and later fetch it and play it back via
>>>>
>>>> print(myplot)
>>>>
>>>>         [[alternative HTML version deleted]]
>>>>
>>>> ______________________________**________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>>>> PLEASE do read the posting guide http://www.R-project.org/**posting-<http://www.R-project.org/posting->
>>>> guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>
>>> ______________________________**________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>>> PLEASE do read the posting guide http://www.R-project.org/**
>>> posting-guide.html<http://www.R-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>> --
>> Brian D. Ripley,                  ripley at stats.ox.ac.uk
>> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~**ripley/<http://www.stats.ox.ac.uk/~ripley/>
>> University of Oxford,             Tel:  +44 1865 272861 (self)
>> 1 South Parks Road,                     +44 1865 272866 (PA)
>> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list