[R] Random number generation
Duncan Murdoch
murdoch at stats.uwo.ca
Tue May 13 19:58:43 CEST 2008
On 5/13/2008 1:38 PM, Greg Snow wrote:
>> -----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).
One problem with this idea is that it makes the very helpful syntax
error messages from source() less useful:
> cppsource('test.R')
Error in source(tmp) :
c:\temp\Rtmpmyoc5a\file678418be.R: unexpected symbol at
8:
9: if (y > 1:2 cat
Duncan Murdoch
>> > 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
>
> ______________________________________________
> 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