Open In App

How to Create a Multi-Line Comment in R

Last Updated : 16 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In R Programming Language multi-line comments are created using the # symbol at the beginning of each line. These comments are typically used for documentation purposes, helping explain your code’s functionality. The R interpreter ignores them during code execution and is useful for providing context or instructions for other developers or yourself.

Creating a Multi-Line Comment in R

Below are the several methods available for creating a multi-line comment in R Programming Language so we will find out these methods.

Multi-Line Comment Using # for Each Line

In this example, we use the # symbol at the beginning of each line to create a multi-line comment in R. These lines prefixed with # are ignored by the R interpreter, making them suitable for adding explanatory notes or descriptions within the code without affecting its execution.

Syntax:

# This is a multi-line comment in R
# Line 1 of the comment
# Line 2 of the comment
# Line 3 of the comment
R
# This is a multi-line comment in R
# It includes printing "GeeksforGeeks" and the result of 1+1

# Print "GeeksforGeeks"
print("GeeksforGeeks")

# Perform addition operation 1+1 and print the result
result <- 1 + 1
print(result)

Output:

[1] "GeeksforGeeks"
[1] 2

Multi-line Comment for Function Documentation

In this example, the multi-line comment is used to document a function. Each line starting with #’ provides information about the function, its parameters (@param), and return value (@return).

Syntax:

#' Description of the function.
#'
#' More detailed description of the function, its purpose, and behavior.
#'
#' @param parameter_name Description of the parameter.
#' @return Description of the return value.
#' @examples
#' Example usage of the function.
#' @export
function_name <- function(parameter_name) {
# Function body
}
R
#' Calculate the square of a number.
#' This function takes a numeric input and returns its square.
#' @param x Numeric value.
#' @return Square of x.
square <- function(x) {
  return(x^2)
}

# input
input_number <- 5
result <- square(input_number)
print(result) 

Output:

[1] 25

Create Multi-line Comment Using Ctrl + Shift + C 

In RStudio, you can create multi-line comments using the Ctrl + Shift + C keyboard shortcut. Here’s how to do it:

  1. Select the lines of code that we want to comment out.
  2. Press Ctrl + Shift + C on your keyboard.
cr

Create a Multi-Line Comment in R

Here we can see we easily comment the line using Ctrl + Shift + C in R Studio.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads