[R] Random number generation

Greg Snow Greg.Snow at imail.org
Tue May 13 19:38:07 CEST 2008


> -----Original Message-----
> From: Esmail Bonakdarian [mailto:esmail.js at gmail.com]
> Sent: Tuesday, May 13, 2008 8:13 AM
> To: Greg Snow
> Cc: Prof Brian Ripley; r-help at stat.math.ethz.ch
> Subject: Re: [R] Random number generation
>
> Greg Snow wrote:
> >> -----Original Message-----
[snip]

> > you could always run your R scripts through the C preproccessor and
>  > have it strip the block comments for you.
>
> Too much work, call me old school, but I like the computer do
> work for me, rather than the other way around :-)

So, have the computer do the work.  Here is a simple function that tells the computer to do the work:

cppsource <- function(file) {
        tmp <- paste(tempfile(), '.R', sep='')
        system(paste('cpp',file,tmp))
        source(tmp)
        unlink(tmp)
}

Now, instead of doing source('myfile.R') do cppsource('myfile.R') and it will automatically do the preproccing to remove the c-style comments (assuming that cpp is on your path, etc.).  If typing the extra 3 characters is too much work, then just name it something else.

Or if you just want to strip the comments in R and don't want to use the c preprocessor then the function:

cssource <- function(file) {
        tmp <- readLines(file)
        tmp2 <- paste(tmp, collapse='\n')
        tmp3 <- gsub('(?s)/\\*.*?\\*/','',tmp2,perl=TRUE)
        source(textConnection(tmp3))
}

Will do that (it will not deal with nested comments or some other special cases, but both the above functions worked for my quick test case).



>
> > Given the complexity of implementing block commenting (even
> deciding on the syntax) and the ease of current work arounds,
> the cost benefit ratio probably puts this very near the
> bottom of the priority list.
>
> I couldn't possibly offer an opinion on that .. I'll happily
> defer to you and the other experts here for this.

I am not really an expert on this.  I just remember being bitten when writing C and trying to comment out a section of code that already had a comment in it.  I would rather have the R core team spending their time on their current priorities than even get involved in a discussion of whether block comments should be able to be nested or not.

You now have at least 4 possible work arounds, hopefully one of them is satifactory,


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



More information about the R-help mailing list