Comments
A comment starts with a hash mark and is a note R ignores, like a sticky note for the reader. It explains what your code does without changing it.
In this page:
Syntax
# This is a comment
x <- value # trailing comment
Comments
A # starts a comment in R, and everything after it on that line is ignored by the interpreter. R has no multi-line comment syntax, so every comment line needs its own #.
Comments should clarify why code does something non-obvious rather than restate what's already clear from the code.
Note:
RStudio and most editors let you comment/uncomment a whole selected block at once, adding # to every line.
Example: Comments
# This script demonstrates comments
x <- 10 # inline comment after code
# print(x) -- this line is commented out and won't run
print(x)
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Trying to use
//or/* */for comments, which are errors in R; only#starts a comment. - Trying to comment a block with a single
#and forgetting each line needs its own#. - Putting
#inside a string like"a#b"and thinking it starts a comment, when inside quotes it is ordinary text.
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: