8

For documentary purposes, I want some code for a plot in the html-output, but not the plot. Later, I have to call the plotting code, and add something to the plot, but only seeing the additional code. I tried this:

```{r non.finished.plotting, eval=FALSE}
    plot(1,type="n")
```
Some explanatory text here in the output:
"This produces an empty plot, and we could now add some points to it manually."

```{r add.layer, fig.width=5, fig.height=5}
<<non.finished.plotting, echo=FALSE>>
points(x=rnorm(100,1,0.1), y=rnorm(100,0.8,0.1) )

```

I have found the echo-notation at Yihui's, but when I knit this, I get an error message in the output.

## Error: plot.new has not been called yet

I also tried fiddling with the chunk options, but I could not find a combination which does what I want. (Sorry, this is very basic, but I did not find something quite like this example.)

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
aae
  • 439
  • 7
  • 18
  • I think you need to add `eval=FALSE` to the second chunk as well. It looks like the first chunk is not evaluated *i.e.* `plot` is never called, but the second chunk is, producing an error since you cannot start a new plot by calling `points`. – Backlin Apr 05 '13 at 11:39
  • Thanks @Backlin, but let me clarify. I do want to call the plot in the second chunk, but not in the first chunk. The only thing I came up with was introducing a third chunk calling the first and than adding points with eval=TRUE, echo=FALSE. But is this really needed? – aae Apr 05 '13 at 13:50
  • Oh, and I forgot to mention: if you don't use echo=FALSE, but just call the chunk, both code and plot are in the output. – aae Apr 05 '13 at 13:58
  • So basically you want to make a series of chunks that display step by step instructions on how to make a plot. Since the first chunk is empty you don't want to display the result from it, but when evaluating the second chunk you need it for `points` to run. I would suggest that you use three chunks: 1 for calling `plot` (echo=TRUE,eval=FALSE); 2 for adding `points` (echo=TRUE,eval=FALSE); and 3 for doing both (echo=FALSE,eval=TRUE). I get the impression this is what you already tried and to the best of my knowledge it is the only way to go. The code gets messy but the output pretty. – Backlin Apr 05 '13 at 14:21

1 Answers1

11

Chunk references in <<>> do not respect chunk options, so <<non.finished.plotting, echo=FALSE>> will not work. What you can do is to move the chunk option echo back to the main chunk like this:

```{r add.layer, fig.width=5, fig.height=5, echo=-1}
<<non.finished.plotting>>
points(x=rnorm(100,1,0.1), y=rnorm(100,0.8,0.1) )
```

echo=-1 means do not echo the first expression (as documented). This is probably what you want:

screen shot of knitr output

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • Thanks a lot, Yihui. That's exacactly what I was looking for, and I totally overlooked it in the doc. Brilliant, this really helps tidying up the code. – aae Apr 06 '13 at 16:18
  • @WilliamBligh you can mark it as the answer if you think this is a correct answer – Yihui Xie Apr 08 '13 at 01:22