Is it possible to have a multi-line comments in R? [duplicate]

0 votes
asked Nov 9, 2010 by tal-galili

This question already has an answer here:

6 Answers

0 votes
answered Jan 9, 2010 by jimmyb

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 ;'.

0 votes
answered Jan 9, 2010 by caracal
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.

0 votes
answered Jan 9, 2010 by banjer

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.

0 votes
answered Nov 9, 2010 by esteis

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) {
         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.

0 votes
answered Jan 26, 2012 by hugh-perkins

Put the following into your ~/.Rprofile file:

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

Now, you can exclude blocks like follows:


0 votes
answered Jan 17, 2013 by soilsciguy

R Studio (and Eclipse + StatET): Highlight the text and use CTRL+SHIFT+C to comment multiple lines in Windows. Or, command+SHIFT+C in OS-X.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter