atiretoo
atiretoo

Reputation: 1902

Running code one line at a time breaks inline chunk output

When I run a complete chunk of code, inline output appears as expected. However if I run the same chunk one line at a time it breaks if I use more than one plot call. So:

set.seed(129485)
x=1:10
y=rnorm(length(x), x, 1)
plot(x,y)
lines(x,y)

works fine in an R script. If I insert it into a code chunk in a brand new R markdown file, like this:

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

This chunk runs if I run the entire chunk using control-shift-enter, the play button, or by selecting all the lines and using control-enter. However, if I run the lines one at a time using control-enter, it breaks on the call to lines(x,y):

Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet

It goes away if I set the options to chunk output in console. I guess it could be the intended behavior, but then maybe control-enter should do the same thing as control-shift-enter when using inline output?

I'm using RStudio 1.0.44 and

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

I can also reproduce this behavior on this machine:

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

Upvotes: 3

Views: 1819

Answers (1)

atiretoo
atiretoo

Reputation: 1902

Kevin Ushey over at the RStudio community forum had this answer:

This is unfortunately a consequence of the Notebook execution model. The graphics device state is reset after each execution, so code execution that is done line-by-line (e.g. to fill a plot) will not work as expected.

There are some workarounds for this:

  1. Prefer executing a whole chunk at a time, using e.g. Cmd + Shift + Enter,
  2. Wrap your plot generating code in brackets {},
  3. Define a function that generates the entire plot,
  4. Disable inline chunk output for that document. (Do this by clicking the Gear icon in the source toolbar and choosing 'Chunk Output in Console')

Upvotes: 1

Related Questions