[R] How to comment in R

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Wed Feb 11 20:45:22 CET 2009

Greg Snow wrote:
> Stavros,
> Thanks for the clarification and going further back in the credit for /* */.  
> I don't think that the regex argument is a complete red herring (possibly a pink herring or burnt umber herring?).  The post I was responding to said that implementing it would be trivial, I think that the fact that Friedl spends 10 pages discussing it shows that it is less than trivial.  

hey, be fair.  i was talking about the perl-style pod block comments,
where it *is* very easy to implement.  just any* line that starts with
=[^\s] (including =cut; the pattern is perhaps just slightly more
complex, but it doesn't really matter here) in a non-block-comment
context starts a block comment, and only a line with =cut\s in a
block-comment context ends a block comment.  as clear as that.  #=head
does not start a block comment, and #foo =cut does not stop a block
comment.  is this really difficult to conceive,  agree on, and implement?

* not inside string delimiters, for example.  already solved in r for
single line comments-like multiline strings.

> While the parser can process the comments without using regular expressions, some of the issues that Friedl brings up still need to be considered in deciding the rules.  Implementing this in the parser may well be trivial once the rules are decided on (but way beyond me), but I still think that deciding on the rules and documenting them is far from trivial.

might be helpful to see some concrete counterexamples.

> I remember having some C code that compiled fine and did as intended with one compiler, then when I tried compiling with a different compiler it threw an error based on the commenting (probably the difference was in the preprocessors, not the compilers), so the 2 different versions of the C compiler/preprocessor did not even agree on the rules (I don't remember emacs complaining either way).

i remember a c++ compiler that would ignore for loops (i had to replace
all of them with while...). 

r is already complex enough, both syntactically and semantically, for
multiline comments to be an outstanding unsurmountable complication.

> Others have mentioned using sed as another way to add/strip comment markers to regions of code.  Along these lines someone could always use C-style (or PL/I style to be more correct but less common in my experience) comments, then run the code through a C preprocessor before submitting to R (then you just have to live with the rules of the preprocessor).

well, cpp is a bit too focused on c.  from man cpp:

" The C preprocessor is intended to be used only with C, C++, and Objec‐
       tive-C source code.  In the past, it has been abused as a general
       processor.  It will choke on input which does not obey C’s lexical

which does not mean you couldn't be able to tweak it to block-commenting
r code.

btw. the sed example was trivialized, it would not treat comment-like
parts of character strings properly -- but sed is not intended as a
generic parser, it's still a line-by-line text processor, with some
features allowing it to consider multiline context, and it's presumably
best to have the r parser do the job.

> I have no problem with someone adding this capability to R, I just prefer that R-Core spends their time on higher priority items.  If someone else wants to contribute the change, I won't complain (unless it breaks my existing code, which is unlikely if done properly), I just wanted anyone who was thinking of doing this to think about some of the potential pitfalls so that if they implement it, they implement it well.

it's *design* that has to be done properly in the first place. 


Wacek Kusnierczyk, MD PhD

Email: waku at idi.ntnu.no
Phone: +47 73591875, +47 72574609

Department of Computer and Information Science (IDI)
Faculty of Information Technology, Mathematics and Electrical Engineering (IME)
Norwegian University of Science and Technology (NTNU)
Sem Saelands vei 7, 7491 Trondheim, Norway
Room itv303

Bioinformatics & Gene Regulation Group
Department of Cancer Research and Molecular Medicine (IKM)
Faculty of Medicine (DMF)
Norwegian University of Science and Technology (NTNU)
Laboratory Center, Erling Skjalgsons gt. 1, 7030 Trondheim, Norway
Room 231.05.060

More information about the R-help mailing list