Save all plots already present in the panel of Rstudio

后端 未结 5 1365
一生所求
一生所求 2021-02-02 12:10

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

5条回答
  •  無奈伤痛
    2021-02-02 12:38

    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.

提交回复
热议问题