1

Using Rstudio to edit an Rmarkdown file, I use HTML comments mark out large blocks that I don't want to have processed or in the output. This works just fine in Rstudio, which ignores everything in the comments. However, when I ask Rstudio to knit the document, knitr is executing the R code blocks in the comments.

Here's an MWE .Rmd file:

# Important stuff I want to see
This is what I want to see:
```{r}
pi # I like pi
```
<!---
**This section commented out, as not ready to be knit yet**
This is what I do not want to see:
```{r}
cake # I also like cake, but it's undefined
```
-->

This causes knitr to fail with Error in eval(expr, envir, enclos) : object 'cake' not found ... Execution halted

Is there an easy way to comment out whole swathes of an Rmarkdown file which prevents knitr from executing the R code chunks in the comments?

I have looked at global comment option for R markdown in knitr and Comments in Markdown, as well as https://yihui.name/knitr/, but didn't find a solution.

chriss
  • 111
  • 6

3 Answers3

1

Taking jburkhardt's idea of using eval=F, this works as a way of doing block comments where knitr does not execute the R code blocks:

Stuff I want to see...
```{r}
pi
```

<!--
This is added at the beginning of the comment:
```{r, include=FALSE}
knitr::opts_chunk$set(eval= FALSE)
```

Stuff I have commented out:

```{r}
cake
```

This is added to the end of the comment:
```{r, include=FALSE, eval=TRUE}
knitr::opts_chunk$set(eval= TRUE)
```
-->

More stuff I want to see:
```{r}
2*pi
```

It's a bit clunky, and it's certainly not bulletproof (for example, knitr will still run any code blocks it finds with explicit eval=true), but it works in my case.

chriss
  • 111
  • 6
0

Select lines before and after the {r} code chunk and use Control/Shift-C (pc) to comment out. You will get this kind of syntax; it won't knit, and won't give an error.

<!-- **This section commented out, as not ready to be knit yet** -->
<!-- This is what I do not want to see: -->
<!-- ```{r} -->
<!-- cake # I also like cake, but it's undefined -->
<!-- ``` -->
akaDrHouse
  • 2,190
  • 2
  • 20
  • 29
  • Well, that works :) It's kind of the nuclear option, though. I guess I am really looking for a block comment rather than a line-wise comment. This is kind of unwieldy for large sections (which I have), and means you have to uncomment line-wise when you copy/paste chunks between the commented section and elsewhere in the document. Any thoughts on a block comment that works with `knitr`? – chriss Jun 08 '17 at 14:10
  • It's marginally better for my purposes to use the html block comment as I had it, and then just add another comment character to the beginning of every line inside that, like a `#`. This is less visual noise since it's only a single character in the left margin, and easier to tidy up when copy-pasting later. But it's still line-wise... (I actually do my editing in vim, but interact with knitr through Rstudio...) – chriss Jun 08 '17 at 14:12
0

In the second code junk you could set eval=F until you have finished this part of your code.

<!--
**This section commented out, as not ready to be knit yet**
This is what I do not want to see:
```{r eval=F}
cake # I also like cake, but it's undefined
```
-->
jburkhardt
  • 675
  • 1
  • 4
  • 18
  • Actually, I like your idea here. It does not scale directly to large sections with multiple code blocks, but setting the default chunk options to be `eval=FALSE` at the head of the comment, and back to `eval=TRUE` at the tail, which just do what I need – chriss Jun 08 '17 at 14:24