Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Comments are portions of a computer program that are used to describe a piece of code. For example,
# declare variable age = 24 # print variable print(age)
Here, # declare variable
and # print variable
are two comments used in the code.
Comments have nothing to do with code logic. They do not get interpreted or compiled and are completely ignored during the execution of the program.
Types of Comments in R
In general, all programming languages have the following types of comments:
- single-line comments
- multi-line comments
However, in R programming, there is no functionality for multi-line comments. Thus, you can only write single-line comments in R.
1. R Single-Line Comments
You use the #
symbol to create single-line comments in R. For example,
# this code prints Hello World print("Hello World")
Output
[1] "Hello World"
In the above example, we have printed the text Hello World
to the screen. Here, just before the print statement, we have included a single-line comment using the #
symbol.
Note: You can also include a single-line comment in the same line after the code. For example,
print("Hello World") # this code prints Hello World
2. R Multi-Line Comments
As already mentioned, R does not have any syntax to create multi-line comments.
However, you can use consecutive single-line comments to create a multi-line comment in R. For example,
# this is a print statement # it prints Hello World print("Hello World")
Output
[1] "Hello World"
In the above code, we have used multiple consecutive single-line comments to create a multi-line comment just before the print statement.
Purpose of Comments
As discussed above, R comments are used to just document pieces of code. This can help others to understand the working of our code.
Here are a few purposes of commenting on an R code:
- It increases readability of the program for users other than the developers.
- Comments in R provide metadata of the code or the overall project.
- Comments are generally used by programmers to ignore some pieces of code during testing.
- They are used to write a simple pseudo-code of the program.
How to Create Better Comments?
As an R developer, your task is not only to write effective code. At times, you may also need to read codes written by other developers and modify them. In such a case, a well-written comment might be a lifesaver.
You should always keep in mind the following points while writing comments.
- Use comments only to describe what a particular block of code does, not how it does.
- Don’t overuse comments. Try to make your code self-explanatory.
- Try to create comments that are as precise as possible.
- Don’t use redundant comments.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.