[R] commenting out a block of R code

David L Carlson dcarlson at tamu.edu
Mon May 7 16:54:33 CEST 2012


In RStudio select the lines to be commented (or uncommented) and press
Ctrl+/ or select comment/uncomment on the Edit menu tab


----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Ted Harding
> Sent: Monday, May 07, 2012 2:27 AM
> To: r-help at r-project.org
> Subject: Re: [R] commenting out a block of R code
> 
> In vim, first move to the top line of the block.
> Then press Shift+V (i.e. upper-case V); this line will then be
> highlighted.
> Then move down (down-arrow key) to the bottom line of the block;
> the whole block will then be highlighted.
> 
> At this stage enter
> 
> :s/^/# /
> 
> (The "g" in Don's sequence is not needed -- it is for global
> substitution within a line).
> 
> This is easy and quick in vim, and does not require entering line
> numbers. I'm not sure now (long time since I used vi) whether
> it also works as stated for vi.
> 
> Ted.
> 
> On 07-May-2012 03:16:36 Don McKenzie wrote:
> > 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
> >
> > ______________________________________________
> > 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.
> 
> -------------------------------------------------
> E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
> Date: 07-May-2012  Time: 08:27:10
> This message was sent by XFMail
> 
> ______________________________________________
> 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.



More information about the R-help mailing list