I've made different plots (more than a hundred) for a project and I haven't capture them on the way (yes it's bad , i know). Now, I need to save them all at once but without running again my script (which takes hours). Is there a way to do so within Rstudio ?
Edit: All the plot are already there and I don't want to run them again.
Although this discussion has been inactive for a while, there are some persons, like myself, who still come across the same problem, and the other solutions don't really seem to even get what the actual question is.
So, hands on. Your plot history gets saved in a variable called
.SavedPlots
. You can either access it directly, assign it to another variable in code or do the latter from the plots window.In R 3.4.2, I could index
ph
to reproduce the corresponding plot in a device. What follows is rather straightforward:ph[index_of_plot_in_history]
.Example:
Note: Sometimes this doesn't happen because of poor programming. For instance, I was using the MICE package to impute many datasets with a large number of variables, and plotting as shown in section 4.3 of this paper. Problem was, that only three variables per plot were displayed, and if I used a png device in my code, only the last plot of each dataset would be saved. However, if the plots were printed to a window, all the plots of each dataset would be recorded.
I am not sure how Rstudio opens the device where the plot are drawn, but I guess it uses
dev.new()
. In that case one quick way to save all opened graphs is to loop through all the devices and write them usingdev.print
.Something like :
where
folder
is the path of the folder where you want to store your graph (could be for examplefolder="~"
if you are in linux and want to store all your graph in your home folder).If you enter the following function all that will follow will be save in a document:
You can also use tiff(), jpg()... see ?pdf
In RStudio, every session has a temporary directory that can be obtained using
tempdir()
. Inside that temporary directory, there is another directory that always starts with"rs-graphics"
and contains all the plots saved as".png"
files. Therefore, to get the list of".png"
files you can do the following:Now, you can copy these files to your desired directory, as follows:
Additional feature:
As you will notice, the
.png
file names are automatically generated (e.g.,0078cb77-02f2-4a16-bf02-0c5c6d8cc8d8.png
). So if you want to number the.png
files according to their plotting order in RStudio, you may do so as follows:Hope it helps.
If your plots are 3d, you can take a snapshot of all your plots and save them as a .png file format.
Or else, the best way is to create a multi-paneled plotting window using the
par(mfrow)
function. Try the followingYou can also use
png
,bmp
,tiff
, andjpeg
functions instead ofpdf
. You can read their advantages and disadvantages and choose the one you think is good for your needs.