RStudio 与普通 R 编辑器 - 保存图

RStudio vs. plain R editor - saving plot

以下代码在纯 R 编辑器(R 自带)中有效,在 RStudio 中无效

pdf('test.pdf')
plot(seq(1:10), seq(1:10))
dev.off()

当我在 RStudio 中 运行 它时,我进入 Acrobat "There was an error opening this document. This file is already open or in use by another application"

如果我使用:

,我会得到同样的错误
pdf('test.pdf')
to_save<-plot(seq(1:10), seq(1:10))
print(to_save)
dev.off()

我有 R 版本 3.3.3 和 RStudio:版本 1.0.136 – © 2009-2016 RStudio, Inc. 这是在 Win 7 上。

有什么解决方法吗?

如果您同意 ggplot2ggsave 是一个不错的选择:

ggplot(data.frame(seq(1:10), seq(1:10)), 
       aes(x = x, y = y)) + 
       geom_point()
ggsave("test.pdf")

它在我的 RStudio 上运行良好。根据我的经验,如果尚未关闭与文件的连接,则打开 pdf 时会出现该消息。你收到消息了吗

null device
          1

执行时dev.off()?