143

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
  • 13,002
  • 8
  • 72
  • 101
aonan zhang
  • 1,503
  • 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
  • This question is widely answered here: https://stackoverflow.com/questions/4823468/comments-in-markdown – Chema_arguez Jan 30 '23 at 09:01

5 Answers5

201

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

<!-- regular html comment --> 

Does this work for you?

user1981275
  • 13,002
  • 8
  • 72
  • 101
  • 7
    oddly, inline r code within those html comments tries to evaluate. e.g., `<-- Some text with \`r mean(x)\` inline code -->` 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
37

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
  • 75,767
  • 19
  • 198
  • 294
24

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
  • 525
  • 7
  • 19
0

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

0

We can use

# Converted R Markdown code chunk in .Rmd file
    
`r ''````{r stream, eval=FALSE}
strm <- FastqStreamer("a.fastq.gz")
repeat {
  fq <- yield(strm)
  if (length(fq) == 0)
    break
  ## process chunk
}

This is found from a https://github.com/rstudio/rmarkdown-book/blob/main/03-documents.Rmd bookdown which is used we want a code block to not execute while knitting.

Does it solves your problem??? if you have any questions please let me know.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31