[R] Multi-line comments?

2 views
Skip to first unread message

Mark Knecht

unread,
Jul 22, 2009, 10:30:58 AM7/22/09
to r-help
Hi,
I looked in the language definition and was surprised. Is there
really no multi-line/block comment defined in R?

I wanted to comment out 20 lines that I'm moving to a function but
didn't want to delete them. Is there no defined way to get around
using a # on each of the 20 lines?

Thanks,
Mark

______________________________________________
R-h...@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.

Gabor Grothendieck

unread,
Jul 22, 2009, 10:50:49 AM7/22/09
to Mark Knecht, r-help
Try
if (FALSE) { ... }

Paul Hiemstra

unread,
Jul 22, 2009, 11:12:08 AM7/22/09
to Mark Knecht, r-help
Hi,

This issue has come up before, and as far as I know there is no
multiline comment. However, there are a few ways of commenting out
larges pieces of code.

- Use a good text editor, Kate (KDE) allows you to select the 20 lines
and press Ctrl-D to comment them all at once.
- Use a setup like:
if(FALSE) {
line1
...
line20
}

And set FALSE to TRUE if you want to let the code be executed.

These are probably not the only ones, but this is what I could think of
right now.

cheers and good luck,
Paul

--
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone: +3130 274 3113 Mon-Tue
Phone: +3130 253 5773 Wed-Fri
http://intamap.geo.uu.nl/~paul

Michael Knudsen

unread,
Jul 22, 2009, 10:54:34 AM7/22/09
to Mark Knecht, r-help
On Wed, Jul 22, 2009 at 4:30 PM, Mark Knecht<markk...@gmail.com> wrote:

>   I wanted to comment out 20 lines that I'm moving to a function but
> didn't want to delete them. Is there no defined way to get around
> using a # on each of the 20 lines?

Just like you, I have been longing for that myself. It seems that the
answer is negative, so I have ended up using

if (1==0)
{
# code goes here
}

although is not really nice to look at.

--
Michael Knudsen
mickn...@gmail.com
http://lifeofknudsen.blogspot.com/

Barry Rowlingson

unread,
Jul 22, 2009, 11:07:44 AM7/22/09
to Mark Knecht, r-help
On Wed, Jul 22, 2009 at 3:30 PM, Mark Knecht<markk...@gmail.com> wrote:
> Hi,
>   I looked in the language definition and was surprised. Is there
> really no multi-line/block comment defined in R?
>
>   I wanted to comment out 20 lines that I'm moving to a function but
> didn't want to delete them. Is there no defined way to get around
> using a # on each of the 20 lines?

Hmmm I think this might be a FAQ... Usual suggestions are:

1. use an editor that can do un/commenting of regions.

2. Stick it in an if(FALSE){ ... } block, but that's only good if
it's syntactically correct.

I prefer:

3. Manage your code under a decent SCM and don't fear the delete.

Barry

Jonathan Baron

unread,
Jul 22, 2009, 11:15:40 AM7/22/09
to Mark Knecht, r-help
On 07/22/09 07:30, Mark Knecht wrote:
> Hi,
> I looked in the language definition and was surprised. Is there
> really no multi-line/block comment defined in R?
>
> I wanted to comment out 20 lines that I'm moving to a function but
> didn't want to delete them. Is there no defined way to get around
> using a # on each of the 20 lines?

This topic has come up before and the answer seems to be no, although
you can do tricky things like defining a function containing the
comment as a quoted string (beginning and ending with quote marks), or
putting a quoted string after an if() for a condition that isn't met.
It helps that line breaks are allowed in quoted strings.

However

With (x)emacs and ESS, you can say
M-x comment-region
after defining a region with point and mark.

I got tired of typing this, so I defined the following in my
.xemacs/init.el and .emacs files:

(global-set-key "\M-r" 'comment-region)

So now I do it with "alt-R". That key may have had some other
purpose, but not one I ever used.

I think the alt key is something else on a Mac. And I have no idea
whether this will work on Windows.

Jon
--
Jonathan Baron, Professor of Psychology, University of Pennsylvania
Home page: http://www.sas.upenn.edu/~baron

Erik Iverson

unread,
Jul 22, 2009, 11:27:56 AM7/22/09
to Michael Knudsen, Mark Knecht, r-help
What editor are you all using to write R code? Many will have ways of doing what you want, e.g., comment-region (bound by default to M-; through comment-dwim) in Emacs.

Michael Knudsen

unread,
Jul 22, 2009, 11:34:43 AM7/22/09
to Erik Iverson, r-help
On Wed, Jul 22, 2009 at 5:27 PM, Erik Iverson<eive...@nmdp.org> wrote:

> What editor are you all using to write R code?  Many will have ways of doing what you want, e.g., comment-region (bound  by default to M-; through comment-dwim) in Emacs.

Cool! I'm using Xcode, and I have just realized that cmd+/ will make a
block comment. By default it adds '//' instead of '#', but I guess
that it can be fixed somehow.

Bert Gunter

unread,
Jul 22, 2009, 12:25:05 PM7/22/09
to Michael Knudsen, Erik Iverson, r-help
You can find a list of IDE's/R code editors for R here:
http://www.sciviews.org/_rgui/projects/Editors.html

However, this is somewhat dated, and you may find others not here just by
googling on "R Editor", "R IDE", etc.

Bert Gunter
Genentech Nonclinical Biostatistics

-----Original Message-----
From: r-help-...@r-project.org [mailto:r-help-...@r-project.org] On
Behalf Of Michael Knudsen
Sent: Wednesday, July 22, 2009 8:35 AM
To: Erik Iverson
Cc: r-help
Subject: Re: [R] Multi-line comments?

Liviu Andronic

unread,
Jul 22, 2009, 12:49:07 PM7/22/09
to Bert Gunter, r-help
Hello,

On Wed, Jul 22, 2009 at 6:25 PM, Bert Gunter<gunter...@gene.com> wrote:
> You can find a list of IDE's/R code editors for R here:
> http://www.sciviews.org/_rgui/projects/Editors.html
>
> However, this is somewhat dated, and you may find others not here just by
> googling on "R Editor", "R IDE", etc.
>

Missing from the list are the cross-platform: Geany, SciViews-K, JGR.
A future addition will be the promising jEditR [1].

Another such list is on Wikipedia. It is also worth searching the ML
archives, since similar discussions popped-up several times.
Liviu

[1] http://romainfrancois.blog.free.fr/index.php?post/2009/07/08/useR!-slides

Mark Knecht

unread,
Jul 22, 2009, 12:57:14 PM7/22/09
to r-help
Thanks Gabor and EVERYONE else who answered so quickly.

Paul Hiemstra

unread,
Jul 22, 2009, 2:27:34 PM7/22/09
to Erik Iverson, r-help
Erik Iverson schreef:

> What editor are you all using to write R code? Many will have ways of doing what you want, e.g., comment-region (bound by default to M-; through comment-dwim) in Emacs.
>
I use Kate, the advanced text editor in KDE.
Paul

> -----Original Message-----
> From: r-help-...@r-project.org [mailto:r-help-...@r-project.org] On Behalf Of Michael Knudsen
> Sent: Wednesday, July 22, 2009 9:55 AM
> To: Mark Knecht
> Cc: r-help
> Subject: Re: [R] Multi-line comments?
>
> On Wed, Jul 22, 2009 at 4:30 PM, Mark Knecht<markk...@gmail.com> wrote:
>
>
>> I wanted to comment out 20 lines that I'm moving to a function but
>> didn't want to delete them. Is there no defined way to get around
>> using a # on each of the 20 lines?
>>
>
> Just like you, I have been longing for that myself. It seems that the
> answer is negative, so I have ended up using
>
> if (1==0)
> {
> # code goes here
> }
>
> although is not really nice to look at.
>
>

______________________________________________

Romain Francois

unread,
Jul 23, 2009, 2:13:28 AM7/23/09
to Liviu Andronic, r-help, Bert Gunter
On 07/22/2009 06:49 PM, Liviu Andronic wrote:
> Hello,
>
> On Wed, Jul 22, 2009 at 6:25 PM, Bert Gunter<gunter...@gene.com> wrote:
>> You can find a list of IDE's/R code editors for R here:
>> http://www.sciviews.org/_rgui/projects/Editors.html
>>
>> However, this is somewhat dated, and you may find others not here just by
>> googling on "R Editor", "R IDE", etc.
>>
> Missing from the list are the cross-platform: Geany, SciViews-K, JGR.
> A future addition will be the promising jEditR [1].
>
> Another such list is on Wikipedia. It is also worth searching the ML
> archives, since similar discussions popped-up several times.
> Liviu
>
> [1] http://romainfrancois.blog.free.fr/index.php?post/2009/07/08/useR!-slides

Thanks for the free marketing... at the moment we are dealing with
installation procedures, so I would not say it is ready for prime time
just yet.

The old list of R editors (and guis) should really be moved away in the
R wiki or something so that people can update it more often, and also
perhaps people could then review/rank their preferred tool.

Romain

--
Romain Francois
Independent R Consultant
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/tlNb : RGG#155, 156 and 157
|- http://tr.im/rw0p : useR! slides
`- http://tr.im/rw0b : RGG#154: demo of atomic functions

Reply all
Reply to author
Forward
0 new messages