No great options to comment out text in R Markdown #4

Open
opened 2023-09-14 15:43:25 +00:00 by solarchemist · 0 comments

When writing in R Markdown format I often would like to comment out one or more lines of text (for example to quickly test variations on sentences or paragraphs) in a way that these comments do not show in the generated HTML file.

The R Markdown Cookbook only mentions HTML comment syntax <!-- your comment --> which does not stop the comment from remaining in the generated HTML file.

Best solutions, as far as I know:

Additional YAML blocks

Add a YAML block with commented out lines (baptiste's suggestion):

Some text in the document.

---
# commented out text
# and such
---

This works fine, it is just slightly verbose to use.
But it is the only option that works without any issues.

This is the most popular answer (by far) on SO:

Some text in the document.

[//]: # (commented out text)

[//]: # (another comment)

According to other answers/comments, this sort of comment line should always have a blank line before it to be proper.
The drawback for Rmd appears as soon as we have more than one such comment line; knitr spits out a very visible warning in the standard output: [WARNING] Duplicate link reference '[//]' at ... line ....

The only(?) way to avoid those warnings is to differentiate the link labels, but that quickly becomes tedious.

Some text in the document.

[//1]: # (commented out text)

[//2]: # (another comment, now without a knitr warning)

If you know of a less verbose method than adding YAML blocks, or an easier to circumvent the knitr warnings, I would love to learn :-)

+ https://stackoverflow.com/questions/17046518/comment-out-text-in-r-markdown-rmd-file + https://stackoverflow.com/questions/4823468/comments-in-markdown When writing in R Markdown format I often would like to comment out one or more lines of **text** (for example to quickly test variations on sentences or paragraphs) in a way that these comments *do not show* in the generated HTML file. The [R Markdown Cookbook only mentions HTML comment syntax](https://bookdown.org/yihui/rmarkdown-cookbook/comments.html) `<!-- your comment -->` which does not stop the comment from remaining in the generated HTML file. Best solutions, as far as I know: ### Additional YAML blocks Add a YAML block with commented out lines ([baptiste's suggestion](https://stackoverflow.com/a/46880897/1198249)): ``` Some text in the document. --- # commented out text # and such --- ``` This works fine, it is just slightly verbose to use. But it is the only option that works without any issues. ### Abuse the markdown link labels syntax [This is the most popular answer (by far) on SO](https://stackoverflow.com/a/20885980/1198249): ``` Some text in the document. [//]: # (commented out text) [//]: # (another comment) ``` According to other answers/comments, this sort of comment line should always have a blank line before it to be proper. The drawback for Rmd appears as soon as we have more than one such comment line; knitr spits out a very visible warning in the standard output: `[WARNING] Duplicate link reference '[//]' at ... line ...`. The only(?) way to avoid those warnings is to [differentiate the link labels](https://stackoverflow.com/questions/4823468/comments-in-markdown#comment132842067_20885980), but that quickly becomes tedious. ``` Some text in the document. [//1]: # (commented out text) [//2]: # (another comment, now without a knitr warning) ``` If you know of a less verbose method than adding YAML blocks, or an easier to circumvent the knitr warnings, I would love to learn :-)
Sign in to join this conversation.
No Label
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: solarchemist.se/main#4
There is no content yet.