๐ŸŽ“ A1Learner
๐Ÿ”
0 of 40 lessons completed

R Comments

A comment is text in a script that R skips when it runs. Comments speak to people who read the script later.

A comment on its own line

A comment sits on its own line above the code. R skips the comment and prints the text.

# A note for the reader
print("Hello")

A comment after code

A comment sits after code on the same line. Only the code part runs.

print("Ready") # This part is a note

Comments change nothing

One print line carries a comment and one does not. Both lines print the same text.

print("Same result") # A note changes nothing
print("Same result")

Example

# The report starts here
print("Report title") # A short title
print("Report body")

Key points

  • A comment starts with the # mark.
  • R ignores all text from the # mark to the end of the line.
  • A comment can fill a whole line on its own.
  • A comment can sit after code on the same line.
  • Comments never change the output of a script.

In this example the first line is a comment and R skips it. The note after the title line changes nothing in the output.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท rdrr.io R Snippets

Exercise

Which mark starts a comment in R?