Comment Out Block of Code in R (3 Examples)


In this tutorial you’ll learn how to comment out an entire block of code in R.

Table of contents:

If you want to know more about these contents, keep reading.


Introduction of Example Code

Let’s assume that we have an R script containing the following code:

x <- 1                            # Some random R code
y <- 2
z <- 3

For some reason, we don’t want to execute this code, but we also don’t want to delete it. In that case, it makes sense to comment out this R code temporarily.

The following examples will show different ways how we can do that quick and efficiently in the R programming language. So keep on reading!


Example 1: Commenting Out Using Keyboard Shortcut

This Example explains how to comment out an entire block of code by putting hashtags in front of each line of the code.

To do this quickly, we first have to highlight the code we want to get rid of and then we have to use the keyboard shortcut Control + Shift + c.

Afterwards, our example code looks as follows:

# x <- 1                          # Some random R code
# y <- 2
# z <- 3

The technique shown in this example is, in my opinion, by far the best and smoothest way to comment out large code blocks. However, I want to show you to other alternatives in the following examples.


Example 2: Commenting Out Using if-Statement

In this Example, I’ll show how to use the if-statement to comment out a huge code block in R.

For this, we have to specify the logical value FALSE within the if-condition. Within the if-statement, we can then insert all the code that we don’t want to execute.

if(FALSE) {                       # Wrap FALSE if statement
  x <- 1                          # Some random R code
  y <- 2
  z <- 3


Example 3: Commenting Out Using Manually Defined Function

In Example 3, I’ll illustrate how to create a user-defined function to comment out a block of R syntax. We simply can store all the code that we don’t want to use within our function:

fun_unused <- function() {        # Wrap function around code
  x <- 1                          # Some random R code
  y <- 2
  z <- 3


Video, Further Resources & Summary

I have recently released a video on my YouTube channel, which shows the R codes of this article. You can find the video below.


The YouTube video will be added soon.


Furthermore, you could read some of the related tutorials of this website. A selection of articles about interesting hacks in R is shown below:


Summary: This post showed how to comment out a code block in the R programming language. If you have any additional questions, don’t hesitate to let me know in the comments.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.