How to Comment in R

Comments are plain text that are not interpreted by the R compiler. They are typically placed to inform the reader about what the script is doing at a particular point. You can use comments to make notes, add titles, and provide instruction to users going through your code. Programmers also use comments to ignore some pieces of code during script testing.

Remember that Comments have a green color in R Studio. This guide will show you how to comment in R.

Single Line Comment in R

To add a single-line comment in R, type the # symbol followed by your comment. For example,

#the code below prints Hello World
print(”Hello World”)

Output

Single Line Comment
Single Line Comment using # symbol.

In this example, we have included a single-line comment using the # symbol to inform the user about its functionality.

Alternatively, you can highlight the line you want to comment on. Then, from the top-level “Code” menu, select the Comment/Uncomment Lines option. Or, press Ctrl + Shift + C in Windows (Cmd + Shift + C in macOS).

Comment Uncomment Lines
From the Code menu, select the Comment Uncomment Lines option to add a comment.

Multiple Line Comment in R

To add a multiple-line comment in R, select the multiple lines on which you want to comment using the mouse.

Multiple Line Comments
Select the multiple lines of code.

Then use the following key bindings:

  • For Windows users: Ctrl + Shift + C
  • For macOS users: Cmd + Shift + C
Shortcut Keys to Comment
Press the shortcut keys to comment the lines.

Alternatively, select the multiple lines on which you want to comment. Then, from the top-level “Code” menu, select the Comment/Uncomment Lines option.

Was this article helpful?

Leave a Comment