【发布时间】:2018-12-06 14:50:01
【问题描述】:
是否可以通过以下方式将绘图自动保存到png格式的文件夹中?
- 每次调用
plot.new()时都会创建一个新的png文件 - 文件会在每次图形更新时更新(例如,当调用
points()、lines()、rect()时),因此每当更新绘图时,文件也会更新(无需关闭设备dev.off())
如果可能的话,下面的代码应该可以正常工作:
plot(rnorm(100))
# create a new png file (e.g. plot-1.png) and the graphics is output to the file
plot(rnorm(100))
# create a new png file (e.g. plot-2.png) and the graphics is output to the file
abline(h = 0, col = "red")
# then the line is output to the file
这样做的动机是为了避免 X11/Quartz 转发(它们不能很好地与 screen/tmux 配合使用,因为当用户从其他地方附加会话时图形会丢失),并避免看起来似乎是 xpra很少有人支持它(导致 Ubuntu 16.04 卡在登录屏幕,请参阅 https://askubuntu.com/questions/930161/ubuntu-16-04-2-cannot-login-after-installing-xpra?noredirect=1#comment1661998_930161)。
这很像 RStudio Server 处理 R 图形的方式(参见https://github.com/rstudio/rstudio/tree/master/src/cpp/r/session/graphics 的源代码)。我想知道是否有更简单的方法来实现这一点(最好不要从头开始重写图形设备)?
【问题讨论】:
-
在绘图之前,调用函数
png。见help("png")。完成后,dev.off()。 -
查看
rmote,这是我通过 ssh/emacs/ess 控制 R 并想查看我的绘图时定期使用的工具。 -
@RuiBarradas 我提到这里不需要
png(...); plot(...); dev.off(),因为我想捕获在会话中创建的所有 图形,而不会将我所有的绘图代码更改为这种样式。期望的结果是,对于 RStudio 用户,绘图很好,但对于终端用户(例如 emacs+ess),绘图应自动重定向到 png 文件,无论我在哪里附加到屏幕/tmux 会话,这些文件都会保留。
标签: r x11 graphic rstudio-server