145

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.

2

5 Answers 5

207

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

<!-- regular html comment --> 

Does this work for you?

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
40

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.

Comments

26

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.

5 Comments

I'm running Windows 10, and for me it was also Ctrl+Shift+C. Thanks!
@EvanRosica, HelloGoodbye, Thank you for your information, I edited my answer to add windows shortcut explicitly.
This may work for people who use RStudio, but not for those who write Rmarkdown code with other editors or IDEs - for instance, I use Notepad++, and this shortcut of course does not help there.
OP has specified in the body text of an R markdown document rather than a code chunk. Adding the hash symbol in body text will result in the text being formatted as a title.
Had put a comment here about this being specific to code chunks, but just tested it in RStudio and it seems it does add HTML comment marks when applied to body text. It might be worth editing this answer to clarify that is the case.
0

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.