Can't we comment a block of code at once in R?

r

#1

Hello,

I am facing a lot of problem in commenting my code. I need to run it multiple times while commenting different blocks of code every time. Is there a way in R to comment whole block of code at once like we do in C++ using /* and */ symbols?

thanks.


#2

Inside a function one can use "[Comments]" i.e apostrophe to enclose multiline comments. Outside function one can use apostrophe but if you run the entire script then the comments would be displayed as a character vector.

hello <- function() {
"
These are some random multiline comments
That are added
"
print(“Hello, world!”)
}


#3

@mukesh,

You can do this-

  1. Select the block of code that you want to comment.
  2. Press Ctrl + Shift + C

I think it is the fastest way to comment a block of code in R! :smile:
You can also use the same steps to un-comment the block of code.
Hope it helps!


#4

@Aditya_Sharma, does that work in vanilla R, or is that a feature available in RStudio and the like?


#5

@anon Still using vanilla R? Why?


#6

@Pierre_Lafortune I’m not, but the way I read it, the OP was looking for a way to do it in R and did not mention any particular environment.


#7

@anon,
Not sure about vanilla R, but I use it extensively in RStudio!