"plot.new has not been called yet" rmarkdown 错误(Rstudio 1.0.44)

"plot.new has not been called yet" error in rmarkdown (Rstudio 1.0.44)

我在 iMac 上使用最新版本的 Rstudio

Version 1.0.44 – © 2009-2016 RStudio, Inc. Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/602.2.14 (KHTML, like Gecko)

我注意到 rmarkdown 文件的笔记本功能。生成图时,不再使用通常的 "Plots window",而是在代码块下方生成图。

下面的代码出现错误:

plot(seq(1,10,1))
abline(a=0,b=1)

错误显示在代码块下方:

Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) : plot.new has not been called yet

但是在knit整个rmarkdown文件的时候,并没有报错

所以我想知道如何避免这个错误:

以下将起作用

{plot(seq(1,10,1))
 abline(a=0,b=1)}

这个也行

plot(seq(1,10,1))+
abline(a=0,b=1)

在 RStudio 中,Preferences -> R Markdown 中有一个设置为 "Show output inline for all R Markdown documents"。要消除错误,请确保未选中此选项。

在带有 R 内核的 jupyter 中,如果 运行 逐行代码,您将看到该错误,就像 XR SC 提到的那样。

这件事发生在我身上,因为我在我的绘图中添加了一个无效参数。在我的例子中,我试图执行:

ggplot(df, aes(x=sales)) + geom_histogram() + title('Plot Title')

并且应该一直在执行:

ggplot(df, aes(x=sales)) + geom_histogram() + ggtitle('Plot Title')

请注意,您必须使用 ggtitle,而不是 title