面板中已经存在的所有绘图

面板中已经存在的所有绘图

本文介绍了保存 Rstudio 面板中已经存在的所有绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为一个项目制作了不同的情节(超过一百个),但我没有在途中捕捉它们(是的,这很糟糕,我知道).现在,我需要一次保存它们,但不要再次运行我的脚本(这需要几个小时).有没有办法在 Rstudio 中这样做?

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 ?

所有情节都已经存在,我不想再次运行它们.

All the plot are already there and I don't want to run them again.

推荐答案

在 RStudio 中,每个会话都有一个可以使用 tempdir() 获取的临时目录.在该临时目录中,还有另一个始终以 "rs-graphics" 开头的目录,其中包含保存为 ".png" 文件的所有绘图.因此,要获取 ".png" 文件列表,您可以执行以下操作:

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")


附加功能:

您会注意到,.png 文件名是自动生成的(例如,0078cb77-02f2-4a16-bf02-0c5c6d8cc8d8.png).因此,如果您想根据它们在 RStudio 中的绘图顺序对 .png 文件进行编号,您可以这样做:

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)

希望有帮助.

这篇关于保存 Rstudio 面板中已经存在的所有绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 06:42