135

In an R Markdown (.Rmd) file, how do you comment out unused text? I'm not referring to the text in the R code chunk, but the general texts, like % in LaTex for example.

user1981275
  • 12,406
  • 8
  • 66
  • 96
aonan zhang
  • 1,413
  • 2
  • 10
  • 6
  • 2
    [Was initially closed as duplicate](https://stackoverflow.com/questions/4823468/comments-in-markdown); re-opening since Rmarkdown can potentially provide alternative options not supported by markdown. – baptiste Oct 23 '17 at 01:42

4 Answers4

187

I think you should be able to use regular html comments:

<!-- regular html comment --> 

Does this work for you?

user1981275
  • 12,406
  • 8
  • 66
  • 96
  • 6
    oddly, inline r code within those html comments tries to evaluate. e.g., `` can result in 'blah not Found' errors on Knit due to the inline code being unexpectedly evaluated. – Brian D May 01 '19 at 14:39
  • I think I remember seeing Yihui Xie mention that having knitr recognize the comment characters would be too difficult because knitr is not responsible for rendering the document into html or pdf. I think the HTML comment characters are only dealt with by pandoc/rmarkdown after knitting. – randy Jul 12 '19 at 21:57
35

Extra yaml blocks can be used anywhere inside the document, and commented out with #

---
title: "Untitled"
output: html_document
---

No comment.

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

Note however that this does not prevent knitr from evaluating inline r code.

baptiste
  • 73,538
  • 17
  • 190
  • 281
22

After drag the lines you want to make comment, press SHIFT+CMD+C (macOS), SHIFT+CTRL+C (Windows). This is the shortcut of R Markdown editor (R Studio) to comment out.

z0nam
  • 505
  • 7
  • 19
0

You can always turn off code by putting it within an if(F){} statement.