Comment in R: How to Use Comments in R Programming

Comments can be used to explain the code, and to make it more readable. The comments can also be used to prevent execution when testing alternative code.

Comment in R

Comments in R are generic English sentences used for code readability, prevent code execution, including resources, or explanation of the code or Metadata of the project. Comments have nothing to do with the logic of the code.

Types of Comments in Programming

There are three types of comments in a programming language.

  1. Single-line comments: This kind of comment needs only one line. 
  2. Multi-line comments: This kind of comment requires more than one line.
  3. Documentation comments: This kind of comment is conscripted for a quick documentation lookup.

Comment Support in R

R language only supports single-line comments and does not support multi-line or documentation comments.

Single-Line Comments in R

R only supports single-line comments drafted by a “#” symbol. Single-line comments are comments that require only one line. 

Syntax

# comment statement

Example

# This is the first comment in this example

data_1 <- 101

if (data_1 > 99) {
 print("You have hit the century")
} else {
 print("You are out in nervous ninty")
}

If you run the above code, then comment does not have any impact on this example.

If you only write the comment in the code, then it will not produce any output.

# This is the first comment in this example

If you run the above code, then it won’t produce any output.

Multiline Comments

As we have mentioned that multiline comments are not supported in R language but we can just insert a # for each line to create multiline comments.

# This is the first comment in this example
# The second comment
# written in
# more than just one line

data_1 <- 101

if (data_1 > 99) {
 print("You have hit the century")
} else {
 print("You are out in nervous ninty")
}

That’s it for the comments in R.

Leave a Comment