132

I found this old thread (from over a year ago), which explains how come R doesn't support a multi-line comments (like /* comment */ of PHP, for example).

I am wondering if this has been resolved in the past year, or if there are other alternatives? (For example, in notepad++ with npptor, you can mark a bunch of lines and press ctrl+q to mark them all as comments, are there similar solutions for other IDE's ?)

2
  • 6
    In RStudio you can just begin your comment with #' and the following lines will be automatically commented. Nov 13, 2017 at 20:50
  • " bla comment bla works! " put between ""
    – babipsylon
    Oct 1, 2021 at 12:54

7 Answers 7

171

R Studio (and Eclipse + StatET):

On Windows, highlight the text and use CTRL+SHIFT+C to comment multiple lines.

For macOS, use command+SHIFT+C.

3
  • 6
    Also works the same way to remove the comments of multiple lines. Jul 3, 2019 at 14:22
  • 3
    This solution does not address the OP's question. This simply places multiple single line comments (#) in front of each line. I believe the OP is looking for something to designate the beginning of a comment and the end of a comment--the equivalent of something like the following in SAS: /*this can be multiple lines of comments and only 4 characters are needed to comment the entire code block*/ Jan 8, 2021 at 21:14
  • 1
    It does answer the question. The OP asked: "I am wondering if this has been resolved in the past year, or if there are other alternatives?". This is an alternative.
    – derelict
    Jan 14, 2021 at 20:14
81

You can, if you want, use standalone strings for multi-line comments — I've always thought that prettier than if (FALSE) { } blocks. The string will get evaluated and then discarded, so as long as it's not the last line in a function nothing will happen.

"This function takes a value x, and does things and returns things that
 take several lines to explain"
doEverythingOften <- function(x) {
     # Non! Comment it out! We'll just do it once for now.
     "if (x %in% 1:9) {
          doTenEverythings()
     }"
     doEverythingOnce()
     ...
     return(list(
         everythingDone = TRUE, 
         howOftenDone = 1
     ))
}

The main limitation is that when you're commenting stuff out, you've got to watch your quotation marks: if you've got one kind inside, you'll have to use the other kind for the comment; and if you've got something like "strings with 'postrophes" inside that block, then there's no way this method is a good idea. But then there's still the if (FALSE) block.

The other limitation, one that both methods have, is that you can only use such blocks in places where an expression would be syntactically valid - no commenting out parts of lists, say.

Regarding what do in which IDE: I'm a Vim user, and I find NERD Commenter an utterly excellent tool for quickly commenting or uncommenting multiple lines. Very user-friendly, very well-documented.

Lastly, at the R prompt (at least under Linux), there's the lovely Alt-Shift-# to comment the current line. Very nice to put a line 'on hold', if you're working on a one-liner and then realise you need a prep step first.

2
  • Hello Esteis - regarding the alt-shift-#, it doesn't work on windows. I use "Esc", and I think it work in a similar way to what you describe. Thanks for all the suggestions.
    – Tal Galili
    Nov 9, 2010 at 20:19
  • 1
    On Linux, the functionality to insert comments like that is probably due to the GNU readline library. If it works for you in R, it will probably work for you in many other apps, such as bash itself. Sep 18, 2014 at 15:45
39

CTRL+SHIFT+C in Eclipse + StatET and Rstudio.

1
  • 1
    Yes! Simplest solution to a language level constraint for multi-line comment is a shortcut!
    – Necronet
    Mar 11, 2020 at 21:44
21
if(FALSE) {
...
}

precludes multiple lines from being executed. However, these lines still have to be syntactically correct, i.e., can't be comments in the proper sense. Still helpful for some cases though.

2
  • 1
    thanks @caracal, very useful if you want to skip a whole block of code while being extremely simple to comment out if you want to process the same block another time ` # if(FALSE){ ` and ` # } `
    – Gabriel123
    Sep 6, 2017 at 11:20
  • Is there any way to modify this so that if FALSE were changed to TRUE it would run multiple lines of code within the curly brackets instead of just the last one?
    – coip
    Apr 1, 2021 at 22:33
13

No multi-line comments in R as of version 2.12 and unlikely to change. In most environments, you can comment blocks by highlighting and toggle-comment. In emacs, this is 'M-x ;'.

8

Put the following into your ~/.Rprofile file:

exclude <-  function(blah) {
    "excluded block"
}

Now, you can exclude blocks like follows:

stuffiwant

exclude({
    stuffidontwant
    morestuffidontwant
})
6

Unfortunately, there is still no multi-line commenting in R.

If your text editor supports column-mode, then use it to add a bunch of #s at once. If you use UltraEdit, Alt+c will put you in column mode.

3
  • UltraEdit also has a Comment Add command on the Edit menu: Alt+e, n
    – Hong Ooi
    Jan 16, 2012 at 4:08
  • 1
    put everything between " "
    – babipsylon
    Oct 1, 2021 at 12:53
  • 1
    That should be the accepted answer.
    – buhtz
    Apr 20 at 8:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.