[R] commenting out a block of R code

Don McKenzie dmck at u.washington.edu
Mon May 7 05:16:36 CEST 2012


in vi (vim too?), in edit mode

:a,bs/^/# /g

inserts "# " at the beginning of lines a through b


On 6-May-12, at 7:41 PM, Joshua Wiley wrote:

> Hi Ranjan,
>
> To me, this is really a text editors job.  Feature-rich editors make
> it trivial, for example in Emacs, you can select a region (whatever
> size you want) and M-x comment-region automatically comments every
> line in that region.  Similarly M-x uncomment-region will uncomment
> every line.  If you were doing this all the time, you could bind some
> keyseries to do it for you.  Vim has something similar, though I
> forget the exact command.
>
> A hack is:
>
> if (FALSE) {
> all the lines
> you want
> to be `commented'
> }
>
> which will leave them unevaluated at least.  Both of these have been
> suggested before on the list, which is probably why Brian Ripley
> suggested searching the archives.
>
> Cheers,
>
> Josh
>
>
> On Sun, May 6, 2012 at 7:31 PM, Ranjan Maitra <maitra at iastate.edu>  
> wrote:
>> Dear friends,
>>
>> Is there an easy way of commenting out a block of R code after it has
>> been written? (I am aware that R-aware editors can insert #
>> line-by-line while it is being written, but I want to basically block
>> out chunks of R code in a few strokes.)
>>
>> This question was asked on this mailing list some time ago: Professor
>> Ripley's answer was to try the following:
>>
>> RSiteSearch(string="comment multiple lines")
>>
>> Perfectly fine, but inexplicably, I got searches back (seven
>> pages) which do not seem to have any connection with what I am  
>> looking
>> for.
>>
>> Is there an easy way of doing this?
>>
>> Many thanks and best wishes,
>> Ranjan
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting- 
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
>
> -- 
> Joshua Wiley
> Ph.D. Student, Health Psychology
> Programmer Analyst II, Statistical Consulting Group
> University of California, Los Angeles
> https://joshuawiley.com/
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting- 
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

In science, 'fact' can only mean 'confirmed to such a degree that it  
would be perverse
to withhold provisional assent.' I suppose that apples might start to  
rise tomorrow,
but the possibility does not merit equal time in physics classrooms.
    -- Stephen Jay Gould



Don McKenzie, Research Ecologist
Pacific Wildland Fire Sciences Lab
US Forest Service
phone: 206-732-7824

Affiliate Professor
School of Environmental and Forest Sciences
University of Washington



More information about the R-help mailing list