运行 代码一次一行打断内联块输出
Running code one line at a time breaks inline chunk output
当我 运行 一段完整的代码时,内联输出按预期出现。但是,如果我 运行 同一块一次一行,如果我使用多个 plot 调用,它就会中断。所以:
set.seed(129485)
x=1:10
y=rnorm(length(x), x, 1)
plot(x,y)
lines(x,y)
在 R 脚本中工作正常。如果我将它插入到一个全新的 R markdown 文件中的代码块中,如下所示:
---
title: "wtf"
author: "Drew Tyre"
date: "December 22, 2016"
output: html_document
---
```{r}
set.seed(129485)
x=1:10
y=rnorm(length(x), x, 1)
plot(x,y)
lines(x,y)
```
如果我使用 control-shift-enter、播放按钮或选择所有行并使用 control-enter 运行 整个块,则此块 运行s。但是,如果我 运行 使用 control-enter 一次一行,它会在调用 lines(x,y)
:
时中断
Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet
如果我在控制台中将选项设置为分块输出,它就会消失。我想这可能是预期的行为,但也许 control-enter 在使用内联输出时应该做与 control-shift-enter 相同的事情?
我正在使用 RStudio 1.0.44 和
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets
[6] methods base
loaded via a namespace (and not attached):
[1] rsconnect_0.4.3 tools_3.3.2 yaml_2.1.14
[4] knitr_1.15.1
我也可以在这台机器上重现此行为:
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.2.3 knitr_1.12.3
RStudio 社区论坛上的 Kevin Ushey 给出了这个答案:
不幸的是,这是笔记本执行模型的结果。每次执行后图形设备状态都会重置,因此逐行执行的代码(例如填充绘图)将不会按预期工作。
有一些解决方法:
- 更喜欢一次执行整个块,例如使用Cmd + Shift + Enter,
- 将您的绘图生成代码放在方括号 {} 中,
- 定义一个生成整个图的函数,
- 禁用该文档的内联块输出。 (通过单击源工具栏中的齿轮图标并选择 'Chunk Output in Console')
当我 运行 一段完整的代码时,内联输出按预期出现。但是,如果我 运行 同一块一次一行,如果我使用多个 plot 调用,它就会中断。所以:
set.seed(129485)
x=1:10
y=rnorm(length(x), x, 1)
plot(x,y)
lines(x,y)
在 R 脚本中工作正常。如果我将它插入到一个全新的 R markdown 文件中的代码块中,如下所示:
---
title: "wtf"
author: "Drew Tyre"
date: "December 22, 2016"
output: html_document
---
```{r}
set.seed(129485)
x=1:10
y=rnorm(length(x), x, 1)
plot(x,y)
lines(x,y)
```
如果我使用 control-shift-enter、播放按钮或选择所有行并使用 control-enter 运行 整个块,则此块 运行s。但是,如果我 运行 使用 control-enter 一次一行,它会在调用 lines(x,y)
:
Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet
如果我在控制台中将选项设置为分块输出,它就会消失。我想这可能是预期的行为,但也许 control-enter 在使用内联输出时应该做与 control-shift-enter 相同的事情?
我正在使用 RStudio 1.0.44 和
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets
[6] methods base
loaded via a namespace (and not attached):
[1] rsconnect_0.4.3 tools_3.3.2 yaml_2.1.14
[4] knitr_1.15.1
我也可以在这台机器上重现此行为:
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.2.3 knitr_1.12.3
RStudio 社区论坛上的 Kevin Ushey 给出了这个答案:
不幸的是,这是笔记本执行模型的结果。每次执行后图形设备状态都会重置,因此逐行执行的代码(例如填充绘图)将不会按预期工作。
有一些解决方法:
- 更喜欢一次执行整个块,例如使用Cmd + Shift + Enter,
- 将您的绘图生成代码放在方括号 {} 中,
- 定义一个生成整个图的函数,
- 禁用该文档的内联块输出。 (通过单击源工具栏中的齿轮图标并选择 'Chunk Output in Console')