[R] How to comment in R

Greg Snow Greg.Snow at imail.org
Wed Feb 11 18:32:48 CET 2009


If it is fairly trivial to implement then go ahead and implement it, patches are always welcome.  But make sure that it does all that is intended and does it properly, I don't think this is as trivial as it first appears.

The =pod and =cut of Perl is simple (but was not originally intended as just comments), but this is limited to the "here is documentation" type comments, not the "skip this code" type comments (since the = needs to be the first thing on a line).  You also run into problems if a space accidently inserted before the = and you cannot comment out part of a line.

I believe that there is already a wishlist entry or possibly upgraded to a todo entry to implement multiline quoting (sometimes called heredocs) where you can specify that the next several lines will the character string of interest (<<END
...
END
in Perl, some shell scripts, and probably others).  If this is implemented, then this would be one approach for the above (but still hard to visually parse if used on code and not clear documentation).

The c-style of /* */ allows both types and you can comment out part of a line, but it is not simple to match and has its own restrictions.  Friedl in his regular expressions book takes 10 pages to develop a pattern to match these (and the final pattern is almost 2 full lines of text in the book).  And this is without allowing nesting.  If we don't allow nesting of comments, then in the future someone is going to comment out a block of code that is not working and they want to skip for now, but that block is going to already have a comment inside which will then screw up the parsing due to the no-nesting rule, then we will have a whole new discussion when that person posts to the list with a complaint that things are not working as expected.  Sticking with # to the end of the line is simple, and with modern editors, easy.

Consider the following code, what parts are commented out and which should be run?

x <- rnorm(100)
/* begin of code that does not work yet, I will come back to this later
y <- rnorm( length(x), mean(x)
txt <- " ************/
z <- runif(20)
/******"
mean( (x+3)*10

end comment */
mean(x)

  

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
> Sent: Wednesday, February 11, 2009 3:47 AM
> To: Stephan Kolassa
> Cc: mihai.mirauta at bafin.de; r-help at r-project.org
> Subject: Re: [R] How to comment in R
> 
> > Hi Mihai,
> >
> > one (very bad style) way would be
> >
> > if (FALSE) {
> >    comment
> >    comment
> >    comment
> > }
> >
> 
> this works only if the enclosed text is syntactically valid r code.
> that
> is, you can't have multiline free text comments done this way, neither
> can
> you temporarily comment out unfinished and unparsable code.
> 
> an extension to the parser that would accept multiline start-end
> comment
> tags, be it c-style /* */, perl-style =pod =cut, whatever, should be
> fairly  trivial to implement.  (the perl-style pod markup is
> particularly
> easy to parse, because it suffices to recognize '^=' on a line, no
> advanced pattern-matching needed.)
> 
> somewhat surprising there is no such functionality there in r (or is
> there
> any?), it would be quite convenient.
> 
> vQ
> 
> ______________________________________________
> 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