Comments in bookdown

As far as I know, lines beginning with a ">" sign are interpreted as comments by RMarkdown.
Indeed, these lines are highlighted in green by RStudio.
With bookdown, however, this doesn't seem to work.
Is there a different markup symbol for comments in bookdown?

The comment symbol is # in R code. You can explicitly set a comment character (or character combination) in an RMarkdown document as a parameter, if you so choose. Bookdown uses pygments, I believe, for syntax highlighting, which might be different from what you use in another RMarkdown-generated thing (e.g. for blogdown I use highlight.js).

Using the # symbol for comments is pretty well-established, so I'd recommend using it over > which often indicates a prompt in the R console.
http://r.789695.n4.nabble.com/How-to-comment-in-R-td882882.html

1 Like

There are a few different things that we could be talking about here. I'm going to disambiguate each one.

Code comments. # marks comments in R code which would be inside of code chunks in an RMarkdown file.

What > does. Outside of the code chunks, lines in markdown beginning with > treated as blockquotes. They get a special highlighting in RStudio, but semantically, they are not comments. Here's how they render in the bookdown book.

Commented out text in RMarkdown. Occasionally, I use actual comments in RMarkdown to comment out text so that it does not appear in the final document. You can highlight RMarkdown text in RStudio and then use Code > Comment/Uncomment Lines to comment out lines of text. E.g.,

<!-- This is a comment. -->
<!-- This is a comment. -->
4 Likes

Thank you all for your helpful comments.
Indeed <!-- my comment --> does the job.
It's a bit tedious to often comment and uncomment, because it consists of multiple characters and needs to enclose the comment, but it's also fine.

Pro tip: make it into a markdown snippet!

2 Likes

I highlight the text and hit Ctrl/Cmd+Shift+C in RStudio. Works to comment and uncomment.

5 Likes

I like to use YAML blocks for this,

3 Likes

Thank you all so much for your helpful comments.
It is great to get so many excellent hints.
tjmahr's hint that Ctrl+Shift+C also works on normal text in Rmd files (great feature!) and baptiste hint on using YAML blocks for commenting out whole passages will make my life a lot easier!

1 Like