How to Create a Multi-Line Comment in R (With Examples)

In R, multi-line comments can be created using the pound sign (#) at the beginning of each line. Each pound sign will denote the end of a line and the beginning of a new comment. This is useful for when you need to write longer comments that span multiple lines. It also makes your code easier to read. To end the comment you simply need to add another pound sign (#) on the line after the last line of the comment.


The easiest way to create a multi-line comment in RStudio is to highlight the text and press Ctrl + Shift + C.

You can just as easily remove the comment by highlighting the text again and pressing Ctrl + Shift + C.

The following example shows how to do this in practice.

Example: Create Multi-Line Comment in R

Suppose we have the following script in RStudio:

The first five rows of the script simply describe what the rest of the script does. Thus, we want to turn these rows into comments.

To turn the first five lines into a multi-line comment, we can highlight each of the first five rows and press Ctrl + Shift + C:

multi-line comment in R

Notice that the # symbol has been placed in front of each line, which indicates that the entire line is a comment.

We can just as easily uncomment the first five rows by highlighting each row and pressing Ctrl + Shift + C once again:

Note that the # symbol has been removed from each of the first five lines, which indicates that each line is no longer a comment.

Note that you can press command + Shift + C to perform the exact same action in OS-X.

The following tutorials explain how to perform other common operations in R:

x