comment in R, comment in RStudio, comment multiple lines in R

Comment out multiple lines in R or uncomment

Here is how to comment out multiple lines in R by using the shortcut Ctrl + Shift + C or other techniques. In RStudio, you can comment and uncomment a block of code by using a shortcut or GUI. Commenting is very useful if you want to keep a peace code for later or experiment with that.


Comment code in R

You can type the number sign, hash, or sometimes called pound sign (#) to make single-line comments in R.

# Sys.Date

Sys.Date() # returns the system's idea of the current date

It works in basic R or RStudio. If you want to comment out multiple lines in R, then it is easier to do that in RStudio, because it has dedicated functionality for that purpose. In base R, you have to make multiple single-line comments.


Comment out multiple lines in R (RStudio)

By using Windows shortcut Ctrl + Shift + C or Mac shortcut Command + Shift + C, you can comment out multiple lines of selected R code.

It is also possible to use RStudio GUI to comment out a block of code. By holding the Alt key and using the cursor, select across the lines of code and use # to comment them out.

edit multiple lines of code ad once in RStudio

This last technique is one of my favorite RStudio tips and tricks that you can find here. It is useful for editing multiple lines at once. There are a couple of good ideas that can help you to organize R code. For example, you can fold part of the code or use code snippets.

fold code in RStudio

Here is another post from this blog that contains examples of how to use RStudio code snippets like in the example below.

timestamp code snippet in RStudio





Leave a Reply

Your email address will not be published. Required fields are marked *