How do you Include code that does not run in R Markdown? (Comments)

Sometimes when working with R Markdown, you may want to include some example code, that the routine does not actually run, but simply displays. This makes it similar to a comment, but it lacks any extra symbols that designate a comment in normal code. Fortunately, R Markdown supplies two ways of including code that will not get run.

Commenting Out Code in R Markdown

The primary way is to use a code chunk, in this case, the disabling code goes before the code you want to display but not run. This is basically similar to a comment, but it lacks any extra symbols, and so does not include any in the printout of the code. Each chunk has the chunk option within brackets and to prevent the code from being run you have to turn the evaluation of the code off for the duration of the chunk. As a result, any code within the chunk will not run or even be checked but simply transferred to the output. It is also possible to set this perimeter globally to prevent code from running outside the chunks.

Why do We Comment Out Code in R Markdown?

The difference between the two main options in preventing a segment of code from running in R Markdown is whether or not you want to run the code by default. If you are going to have run the code turned off by default, there is some extra code that needs to be added. Once that is done to run a set of code you need to use a chunk option to turn the evaluation back on for the duration of a particular code chunk. The approach you use will largely depend upon why you are preventing the code from running. In most cases, you only want to turn it off for specific segments of code.

Examples of Commenting Out Code Chunks

Here are two examples of each type of code chunk, the first one for a single chunk and the second example is for the global setting. These examples illustrate both approaches that we have mentioned.

“`{r, eval=FALSE}, echo=TRUE}
print(“Not to be run”)

This code chunk sets the program so that the rest of the code in the chunk is not evaluated but shows up in the program’s output. This locally sets the condition we are looking for.

“`{r setup, include=FALSE} knitr::opts_chunk$set(eval = FALSE)
print(“Not to be run”)
In this code chunk, we globally set the condition that we are looking for where the code does not run, but it does show up in the output.

Application of Including Comments in R Markdown

One of the main applications of these code chunks is troubleshooting a program. It is a way of blocking out a specific segment of code while looking for problems. This is because it is a technique that allows you to effectively remove a segment of code from the program without actually removing it. The consequence is that it allows you to see what happens to the routine when you remove a particular segment of code. Another application is the situation where you want to illustrate a segment of code in your output without actually running it. One case where you would do this is the case where you are referring to an improperly written piece of code.

When you are running a program using R Markdown, being able to send a section of code to your output without running it is a handy tool for both evaluation and illustration of programming code. When using R Markdown, it is a handy tool for helping you to produce the results you want.

Scroll to top
Privacy Policy