Skip to main content
v2026.11,986 entries · CC-BY 4.0

Comment in R: # Syntax, Multi-Line Comments and Shortcuts

How to comment in R: the # rule from the R Language Definition, multi-line comments with no block syntax, and the RStudio comment shortcut.

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.”

  1. Open an R script and select the lines you want to switch off, or place the cursor on one line.
  2. Choose Code > Comment/Uncomment Lines, or press the keyboard shortcut from the table below.
  3. 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

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

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Comment in R: # Syntax, Multi-Line Comments and Shortcuts

2 free questions a day, no account.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →