How to add comments in R

How to add Comments in R

Comments are generic English sentences, mostly written in a program to explain what it does or what a piece of code is supposed to do. More specifically, information that programmer should be concerned with and it has nothing to do with the logic of the code. They are completely ignored by the compiler and are thus never reflected on to the input.

The question arises here that how will the compiler know whether the given statement is a comment or not?
The answer is pretty simple. All languages use a symbol to denote a comment and this symbol when encountered by the compiler helps it to differentiate between a comment and statement.

Comments are generally used for the following purposes:

  • Code Readability
  • Explanation of the code or Metadata of the project
  • Prevent execution of code
  • To include resources

Types of Comments
There are generally three types of comments supported by languages, namely-

  • Single-line Comments- Comment that only needs one line
  • Multi-line Comments- Comment that requires more than one line.
  • Documentation Comments- Comments that are drafted usually for a quick documentation look-up

Note: R doesn’t support Multi-line and Documentation comments. It only supports single-line comments drafted by a ‘#’ symbol.

Comments in R

As stated in the Note provided above, currently R doesn’t have support for Multi-line comments and documentation comments. R provides its users with single-lined comments in order to add information about the code.

Single-Line Comments in R

Single-line comments are comments that require only one line. They are usually drafted to explain what a single line of code does or what it is supposed to produce so that it can help someone referring to the source code.
Just like python single-line comments, any statement starting with “#” is a comment in R.

Syntax:

 # comment statement 

Example 1:filter_none

brightness_4

# geeksforgeeks

The above code when executed will not produce any output, because R will consider the statement as a comment and hence the compiler will ignore the line.

Example 2:filter_none

brightness_4

# R program to add two numbers  # Assigning values to variables a <- 9b <- 4 # Printing sum print(a + b)

Output:

[1] 13
Commenting Multiple Lines

As stated earlier that R doesn’t support multi-lined comments, but to make the commenting process easier, R allows commenting multiple single lines at once. There are two ways to add multiple single-line comments in R Studio:

  • First way: Select the multiple lines which you want to comment using the cursor and then use the key combination “control + shift + C” to comment or uncomment the selected lines.
  • Second way: The other way is to use the GUI, select the lines which you want to comment by using the cursor and click on “Code” in menu, a pop-up window pops out in which we need to select “Comment/Uncomment Lines” which appropriately comments or uncomment the lines which you have selected.

This makes the process of commenting a block of code easier and faster than adding # before each line one at a time.

Help with any types of dissertations

Undergraduate dissertation

Choose from hundreds of experts who can assist you in completing your undergraduate dissertation! Prices start at $10 per page, with potential discounts for longer orders or extended deadlines

Master’s dissertation

 

If you are in the process of completing a Master’s degree, we can provide you with an experienced writer to finish your dissertation. We strive to offer a quick turnaround on tailored papers at an affordable price, starting at $10.30 per page.

Ph.D. or doctoral dissertation

 

Hire from among our most skilled experts to save your time and ease your workload. Prices for Ph.D. assistance start at $10.60 per page.

Dissertation Writing Help
Need Our Services?
Thesis Writing Help
Editing & Proofreading Services
Need Help, Whatsapp Us Now