[R] Is there a construct for conditional comment?

Greg Snow Greg.Snow at imail.org
Thu Aug 20 19:14:05 CEST 2009


That works if everything within the brackets is proper R code (though  if(FALSE) reads better to me).

But sometimes this (or similar) question is asked when the user wants to comment out a section of code that is currently not working, or someone may even want to do something like:

y <- rnorm( n, 
#if 0
0, 1
#else
mu, sigma
#endif
)

Which would work with a preprocessor, but be more difficult with the run time if statement.  But you are correct that for some uses (we don't know what the original poster intends to use this for) a simple run time if statement would work.

-- 
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 Steve Jaffe
> Sent: Thursday, August 20, 2009 9:54 AM
> To: r-help at r-project.org
> Subject: Re: [R] Is there a construct for conditional comment?
> 
> 
> Why not
> 
> if ( 0 ) {
> commented with zero
> } else {
> commented with one
> }
> 
> 
> Greg Snow-2 wrote:
> >
> > I believe that #if lines for C++ programs is handled by the
> preprocessor,
> > not the compiler.  So if you want the same functionality for R
> programs,
> > it would make sense to just preprocess the R file.
> >
> >> In C++, I can use the following construct to choice either of the
> two
> >> blocks the comment but not both. Depending on whether the number
> after
> >> "#if" is zero or not, the commented block can be chose. I'm
> wondering
> >> if such thing is possible in R?
> >>
> >> #if 0
> >> commented with 0
> >> #else
> >> commented with 1
> >> #endif
> >>
> >> Regards,
> >> Peng
> >>
> >
> 
> --
> View this message in context: http://www.nabble.com/Is-there-a-
> construct-for-conditional-comment--tp25034224p25064798.html
> Sent from the R help mailing list archive at Nabble.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.




More information about the R-help mailing list