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”)
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).
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.
Then use the following key bindings:
- For Windows users: Ctrl + Shift + C
- For macOS users: Cmd + Shift + C
Alternatively, select the multiple lines on which you want to comment. Then, from the top-level “Code” menu, select the Comment/Uncomment Lines option.