Quantcast

commenting out a block of R code

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

commenting out a block of R code

Ranjan Maitra
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: commenting out a block of R code

Joshua Wiley-2
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 <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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/

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: commenting out a block of R code

Don McKenzie-2
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 <[hidden email]>  
> 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
>>
>> ______________________________________________
>> [hidden email] 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/
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: commenting out a block of R code

Ranjan Maitra
In reply to this post by Joshua Wiley-2
Hi Joshua and Don,

Thanks very much!

I guess I can now see why one could do that using the editor, but I
like Joshua's hack suggestion. I did not think about it:-(


Best wishes,
Ranjan

On Sun, 6 May 2012 19:41:01 -0700 Joshua Wiley <[hidden email]>
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 <[hidden email]> 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
> >
> > ______________________________________________
> > [hidden email] 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/
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: commenting out a block of R code

ted.harding-3
In reply to this post by Don McKenzie-2
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 <[hidden email]>  
>> 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
>>>
>>> ______________________________________________
>>> [hidden email] 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/
>>
>> ______________________________________________
>> [hidden email] 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
>
> ______________________________________________
> [hidden email] 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) <[hidden email]>
Date: 07-May-2012  Time: 08:27:10
This message was sent by XFMail

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: commenting out a block of R code

David Carlson
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: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Ted Harding
> Sent: Monday, May 07, 2012 2:27 AM
> To: [hidden email]
> 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 <[hidden email]>
> >> 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
> >>>
> >>> ______________________________________________
> >>> [hidden email] 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/
> >>
> >> ______________________________________________
> >> [hidden email] 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
> >
> > ______________________________________________
> > [hidden email] 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) <[hidden email]>
> Date: 07-May-2012  Time: 08:27:10
> This message was sent by XFMail
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Loading...