Comments in R



In this tutorial, we will learn how to write comments in R programming language. We will go through the different types of comments and how to use them in a program.


What are Comments

Comments in R are non-executable statements that are used to annotate the code, provide explanations, and make it more readable. They are ignored by the R interpreter during execution.


Syntax

The syntax to define comments in R is:

# Single line comment

# Multi-line comment
# continues here


Example 1: Single line comments

In R, single-line comments are denoted by the hash symbol (#) and are used to add explanations or annotations within code, ignored by the R interpreter. Single line comments in R start with # and continue to the end of the line. They are used to add short explanations or notes within the code.

For example,

  1. We write a program with a single line comment in the first line.
  2. We use this comment to tell what print() is doing.

R Program

# Print Hello, World! to the console
print('Hello, World!');

Output

[1] 'Hello, World!'


Example 2: Multi-line comments

Multiline comments in R start with # and continue on each line. They can span multiple lines and are used for longer explanations or to comment out blocks of code.

For example,

  1. We write a program with a multiline comment spanning the first two lines.
  2. We use this comment to tell what print() is doing.

R Program

# Print Hello, World! to the console
# This is a multi-line comment
print('Hello, World!');

Output

[1] 'Hello, World!'