7

The recommendation for commenting in .Rmd documents to use HTML commenting <!-- comment here --> is insufficient. I'd like to comment out a section of my document that includes inline evaluations:

I haven't defined `x` yet.

<!-- So when I say that `x` is `r x` in the text, I'd like to comment it out -->

Knitting this fails:

#   |.................................................................| 100%
#    inline R code fragments
# 
# 
# 
# 
# processing file: test.Rmd
# Quitting from lines 2-3 (test.Rmd)
# Error in eval(expr, envir, enclos) : object 'x' not found
# Calls: <Anonymous> ... in_dir -> inline_exec -> withVisible -> eval -> eval
# Execution halted

One option is to comment each inline section:

I haven't defined `x` yet.

So when I say that `x` is `r #x` in the text, I'd like to comment it out

But this suffers if I'd like to comment out a whole paragraph with several such inline calculations, for example. Is there a more canonical way of doing this?

Community
  • 1
  • 1
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
  • Not entirely sure I understand, but if the issue is to comment out many inline R statements, maybe a keyword search and replace in a text editor? Something like *select paragaph* then `Ctrl-f > find "\`r" replace "\`r #"`. – lmo May 04 '17 at 13:58
  • @lmo i mean, sure. I guess maybe this is more of a feature request then.... is there any reason for `knitr` not to recognize the code is embedded in a comment and skip evaluation? – MichaelChirico May 04 '17 at 14:03
  • That is a good question. I just tried it out and see what you mean. It is strange that comments are ignored in this context. – lmo May 04 '17 at 14:16
  • 3
    I think `knitr` goes first and runs all the inline R code before the doc is made to an html so the html comments are pbly ignored at this stage. Not sure how to fix it though. – NicE May 04 '17 at 14:17
  • @NicE yea, that's what I figured. I just don't see why. Filing an issue... – MichaelChirico May 04 '17 at 14:57
  • 1
    OK, this has come up in the past: at [`knitr`](https://github.com/yihui/knitr/issues/1363) and at [`rmarkdown`](https://github.com/rstudio/rmarkdown/issues/974) – MichaelChirico May 04 '17 at 14:59

2 Answers2

6

As @NicE said, knitr goes first to evaluate your code, in particular because there may be inline R code evaluation or other R variable dependent text, which then need to be evaluated as markdown syntax. For instance, this included in rmarkdown:

Define if bold `r bold <- TRUE`  
This text is `r ifelse(bold, "**bold**", "_italic_")`.

Gives:

Define if bold
This text is bold.

Then, I think the only way to insert comments without evaluating them is to embed them in a chunk with eval=FALSE & echo=FALSE

```{r, eval=FALSE, echo=FALSE}
So when I say that `x` is `r x` in the text, I'd like to comment it out
```
Sébastien Rochette
  • 6,536
  • 2
  • 22
  • 43
1

I find that yaml blocks make much nicer comments,

---
title: "Untitled"
author: "baptiste"
date: "10/21/2017"
output: html_document
---

I haven't defined `x` yet.

---
# here's a comment
# ```{r}
# x = pi
# ```
---

unfortunately it doesn't seem to work with inline r code, which is parsed earlier.

baptiste
  • 75,767
  • 19
  • 198
  • 294