Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
To write a comment in R, start the text with a #. R ignores everything from the # to the end of that line. R has no block-comment syntax, so a multi-line comment is simply one # at the start of each line; in RStudio you can add or remove those marks on a whole selection with Code > Comment/Uncomment Lines.
What comments are used for in research code
In an analysis script, comments record why a step exists, which dataset it reads and what a result should look like, so a collaborator or a reviewer can follow the script without you. They are also the quickest way to switch a line off while you debug. Comments do not change results: the R Language Definition states that “Comments in R are ignored by the parser.” Good commenting supports the practices described in computational reproducibility.
How comments work in R
The # rule (comment in R, comments in R)
The R Language Definition (section 10.2, Comments) says: “Any text from a # character to the end of the line is taken to be a comment, unless the # character is inside a quoted string.” Two consequences follow:
- A comment can sit on its own line or after code on the same line.
- The comment always ends at the end of the line; there is no closing marker.
# Whole-line comment
x <- c(2, 4, 6) # trailing comment: R still runs the code before the #
mean(x)
Does # inside a string start a comment?
No. Per the same rule, a # inside a quoted string is part of the string, not a comment. For example, label <- "Sample #3" stores the full text Sample #3. The same logic is why a # in a file you read can matter: the comment.char and comment arguments of the file-reading functions are a separate feature, covered in reading a CSV file into R.
Multi-line comments in R (r comment, multiline)
R has no block-comment delimiter such as /* ... */. The only comment marker is #, and it applies to one line. To comment several lines, put a # at the start of each:
# This block explains the model below.
# Outcome: systolic blood pressure.
# Predictors: age and treatment group.
fit <- lm(sbp ~ age + group, data = trial)
Install or access (official source only)
R itself is distributed from the Comprehensive R Archive Network at cran.r-project.org, which also hosts the R Language Definition and the other official manuals. RStudio is the IDE published by Posit; its documentation is at docs.posit.co/ide/user. Comments work the same way in the plain R console, in a script and in RStudio, because the rule belongs to the language, not the editor.
Step-by-step: comment and uncomment code in RStudio
The RStudio IDE User Guide says RStudio “provides the ability to comment and uncomment entire selections of code.”
- Open an R script and select the lines you want to switch off, or place the cursor on one line.
- Choose Code > Comment/Uncomment Lines, or press the keyboard shortcut from the table below.
- RStudio adds a
#to each selected line. Run the same command on the same selection to remove the marks again.
| Action in RStudio | Windows and Linux | Mac |
|---|---|---|
| Comment/uncomment current line or selection | Ctrl+Shift+C | Shift+Command+C |
| Reflow comment | Ctrl+Shift+/ | Ctrl+Shift+/ |
| Insert code section | Ctrl+Shift+R | Shift+Command+R or Ctrl+Shift+R |
Source: Posit, RStudio IDE User Guide, keyboard shortcuts reference (docs.posit.co/ide/user/ide/reference/shortcuts.html), checked 2026-10-29.
Output and how to read it
A commented line produces no output and no error: R skips it. If you comment out a line that creates an object, later lines that use that object will fail with an “object not found” error, which is the usual sign that you commented out more than intended. Trailing comments leave the code before the # running as normal, as in the mean(x) example above, where x is still created.
Troubleshooting
The shortcut does nothing
The shortcut and the Code menu command act on the active editor pane. Click inside the script first. The commands are RStudio features, so they will not exist in a plain R console or another editor; there, type the # yourself.
My # inside quotes is not hidden
That is the language rule at work: inside a quoted string a # is ordinary text, so it neither starts a comment nor hides the rest of the line.
I commented out a block but R still reports an error
Check that every line in the block starts with #. A single uncommented line in the middle of a block is still run as code.
Ways to comment multiple lines in R
| Method | What it does | Source |
|---|---|---|
# at the start of every line |
The only comment syntax; ignored by the parser | R Language Definition, section 10.2 |
| Code > Comment/Uncomment Lines in RStudio | Adds or removes # on a whole selection |
RStudio IDE User Guide, Text Editor |
| Reflow Comment in RStudio | Re-wraps comment text (Ctrl+Shift+/) | Posit keyboard shortcuts reference |
Related guides
Once your script is commented, see summary statistics in R for a first analysis step and qnorm in R for a worked function example.
FAQ
How do you comment in R?
Put a # before the text. Everything after it on that line is ignored by R.
How do you write comments in R across several lines?
R has no block-comment syntax, so add a # to the start of each line. In RStudio, select the lines and use Code > Comment/Uncomment Lines.
What is the R comment shortcut?
In RStudio it is Ctrl+Shift+C on Windows and Linux and Shift+Command+C on Mac, per the Posit shortcuts reference.
Can a comment go on the same line as code?
Yes. The code before the # runs and the rest of the line is ignored.
Sources
- R Core Team, R Language Definition, section 10.2 Comments, checked 2026-10-29.
- Posit, RStudio IDE User Guide: Text Editor, checked 2026-10-29.
- Posit, RStudio IDE keyboard shortcuts, checked 2026-10-29.







