jeroen00ms
2011-Jul-11 16:08 UTC
[R] Save generic plot to file (before rendering to device)
I am looking for a way to save a plot (graphics contents) to a file after the plot has been calculated but before it has been rendered. More specifically, assume that I made a plot that took a very long time to produce, I would like to save this plot to a generic file that I can later, on a different machine, render to either PDF, PNG, SVG using the usual R graphics devices, without recalculating the graphical contents. As a toy example, suppose this is my plot function: testplot <- function(){ Sys.sleep(10); #very long and complicated procedure plot(cars); } So the use case is that after running testplot() which took potentially 30 days to calculate, I would like to send a file to my colleagues that they can load in R and send to their png or pdf or svg devices just as if they would have made the plot themselves, without having to re-run the code. -- View this message in context: http://r.789695.n4.nabble.com/Save-generic-plot-to-file-before-rendering-to-device-tp3659999p3659999.html Sent from the R help mailing list archive at Nabble.com.
John Sorkin
2011-Jul-11 17:33 UTC
[R] Save generic plot to file (before rendering to device)
After the plot is created, you can save the graph as a pdf document (file menu, save as). You can then send the pdf file to your colleagues. John John David Sorkin M.D., Ph.D. Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing)>>> jeroen00ms <jeroen.ooms at stat.ucla.edu> 7/11/2011 12:08 PM >>> I am looking for a way to save a plot (graphics contents) to a file after the plot has been calculated but before it has been rendered. More specifically, assume that I made a plot that took a very long time to produce, I would like to save this plot to a generic file that I can later, on a different machine, render to either PDF, PNG, SVG using the usual R graphics devices, without recalculating the graphical contents. As a toy example, suppose this is my plot function: testplot <- function(){ Sys.sleep(10); #very long and complicated procedure plot(cars); } So the use case is that after running testplot() which took potentially 30 days to calculate, I would like to send a file to my colleagues that they can load in R and send to their png or pdf or svg devices just as if they would have made the plot themselves, without having to re-run the code. -- View this message in context: http://r.789695.n4.nabble.com/Save-generic-plot-to-file-before-rendering-to-device-tp3659999p3659999.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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. Confidentiality Statement: This email message, including any attachments, is for th...{{dropped:6}}
David Winsemius
2011-Jul-11 18:15 UTC
[R] Save generic plot to file (before rendering to device)
On Jul 11, 2011, at 12:08 PM, jeroen00ms wrote:> I am looking for a way to save a plot (graphics contents) to a file > after the > plot has been calculated but before it has been rendered. More > specifically, > assume that I made a plot that took a very long time to produce, I > would > like to save this plot to a generic file that I can later, on a > different > machine, render to either PDF, PNG, SVG using the usual R graphics > devices, > without recalculating the graphical contents. > > As a toy example, suppose this is my plot function: > > testplot <- function(){ > Sys.sleep(10); #very long and complicated procedure > plot(cars); > }> ?Devices > ?capabilities > capabilities() jpeg png tiff tcltk X11 aqua http/ftp sockets TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE libxml fifo cledit iconv NLS profmem cairo TRUE TRUE TRUE TRUE TRUE TRUE TRUE I have all of the requested capabilities (and more.)> > So the use case is that after running testplot() which took > potentially 30 > days to calculate,Unfortunately you basically threw away all of the intermediate steps by plot()-ting at the very end, since that function returns NULL. If you had plotted first and then executed return(data_object) and then did no operations, you would be able to capture the last calculation with: data_object <- .Last.value Plotting with lattice or ggplot may leave a plot object in the workspace. If done within a function you will need to return it so it doesn't disappear.> I would like to send a file to my colleagues that they > can load in R and send to their png or pdf or svg devices just as if > they > would have made the plot themselves, without having to re-run the > code.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.> > > -- > View this message in context: http://r.789695.n4.nabble.com/Save-generic-plot-to-file-before-rendering-to-device-tp3659999p3659999.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Jeroen Ooms
2011-Jul-16 11:42 UTC
[R] Save generic plot to file (before rendering to device)
> > 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: 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@imail.org >> 801.408.8111 >> >> >> -----Original Message----- >>> From: r-help-bounces@r-project.org [mailto:r-help-bounces@r- >>> project.org] On Behalf Of Jeroen Ooms >>> Sent: Monday, July 11, 2011 12:22 PM >>> To: David Winsemius >>> Cc: r-help@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@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@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@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]]
Uwe Ligges
2011-Jul-16 19:07 UTC
[R] Save generic plot to file (before rendering to device)
On 16.07.2011 20:17, Hadley Wickham wrote:>>> 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. > > It should be possible to do this with a before.plot.new hook, right? > > Hadley >Yes, sure, if you treat the first and last plot separately. But then, I doubt changing "myfn" generates less efforts than inserting the proposed hook. Best, Uwe