Re: [R] Save generic plot to file (before rendering to device)
Bumping this one up because the 'before.plot.new' solution turned out to be sub-optimal after all. It should be possible to do this with a before.plot.new hook, right? Yes, sure, if you treat the first and last plot separately. It turns out that the before.plot.new hook does not is not triggered at the right moments. I'm not sure if this is intended behavior or incorrect implementation. What I was expecting is a hook/event that is triggered every time before a new graphics frame is opened. E.g. if there is an open PDF device and some plots are printed, the number of times the hook is called should be exactly equal to the number of pages in the resulting PDF document. Sometimes this works as expected, sometimes it doesn't. At the end of this message some example code. In the first example, the hook works as expected is called 4 times, as there are 4 plots. In all the other examples the event is either triggered too often or not triggered at all. I guess the hook is called when the plot.new() function is explicitly called, which might not always happen. My question would be if (1) this is the intended behavior for 'before.plot.new', and (2) if yes, would it be possible to define an additional event that always triggers, and only triggers, if a completely new graphics device is opened. I.e. whenever a pdf device would start a new page. Thank you. #set the hook (event listener) setHook(before.plot.new, NULL); setHook(before.plot.new, function(){ message(Yay! A new plot!)}); #works as expected: plot(lm(speed~dist, cars), ask=F); #triggered way too often, once for every partition of the plot plot(mtcars); #not triggered at all by lattice library(lattice); dotplot(speed~dist, cars); #not triggered at all by ggplot2 library(ggplot2); qplot(speed, dist, data=cars); __ R-help@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.
Re: [R] Save generic plot to file (before rendering to device)
Thanks I didn't know about that. I ended up with something like this. Is there a more elegant way to do it? myplots - list(); hasplots - FALSE; setHook(before.plot.new, function(...) { if(hasplots == FALSE){ hasplots - TRUE; } else { myplots[[length(myplots)+1]] - recordPlot(); } }); myfn - function(mylm){ plot(mylm, ask=F) } mylm - lm(dist~speed, data=cars); pdf(tempfile()) dev.control(displaylist=enable) myfn(mylm); if(hasplots){ myplots[[length(myplots)+1]] - recordPlot(); } dev.off() On Sat, Jul 16, 2011 at 8:17 PM, Hadley Wickham had...@rice.edu 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 -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/ __ R-help@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.
Re: [R] Save generic plot to file (before rendering to device)
On Jul 11, 2011, at 22:08 , Greg Snow wrote: Note the warning on the help page for recordPlot. If your colleague is using a different version of R than you there could be problems. Also notice that the process isn't perfect, as people occasionally discover when they use dev.copy() and its relatives like dev.print() or dev.copy2pdf(). Some thing are just hopelessly dependent on the current device and its current parameters. The most obvious case is if you have a text string and a rectangle just big enough to contain it. Different device, different font metrics, etc -- Peter Dalgaard Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@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.
Re: [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.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@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-helphttps://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-helphttps://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, rip...@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, UKFax: +44 1865 272595 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Save generic plot to file (before rendering to device)
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.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@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-helphttps://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-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.htmlhttp://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Brian D. Ripley, rip...@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, UKFax: +44 1865 272595 [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] Save generic plot to file (before rendering to device)
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 -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/ __ R-help@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.
Re: [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 __ R-help@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.
Re: [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.o...@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-tp365p365.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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}} __ R-help@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.
Re: [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 tifftcltk X11 aqua http/ftp sockets TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE libxml fifo clediticonv NLS profmemcairo 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-tp365p365.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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 __ R-help@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.
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 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Save generic plot to file (before rendering to device)
Note the warning on the help page for recordPlot. If your colleague is using a different version of R than you there could be problems. 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.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@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 PLEASE do read the posting guide 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 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.