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.
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:
plots.dir.path <- list.files(tempdir(), pattern="rs-graphics", full.names = TRUE); plots.png.paths <- list.files(plots.dir.path, pattern=".png", full.names = TRUE)
Now, you can copy these files to your desired directory, as follows:
file.copy(from=plots.png.paths, to="path_to_your_dir")
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:
plots.png.detials <- file.info(plots.png.paths) plots.png.detials <- plots.png.detials[order(plots.png.detials$mtime),] sorted.png.names <- gsub(plots.dir.path, "path_to_your_dir", row.names(plots.png.detials), fixed=TRUE) numbered.png.names <- paste0("path_to_your_dir/", 1:length(sorted.png.names), ".png") # Rename all the .png files as: 1.png, 2.png, 3.png, and so on. file.rename(from=sorted.png.names, to=numbered.png.names)
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With