Re: [R] Random number generation

From: Duncan Murdoch <>
Date: Tue, 13 May 2008 13:58:43 -0400

On 5/13/2008 1:38 PM, Greg Snow wrote:

>> -----Original Message-----
>> From: Esmail Bonakdarian []
>> Sent: Tuesday, May 13, 2008 8:13 AM
>> To: Greg Snow
>> Cc: Prof Brian Ripley;
>> 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
> (801) 408-8111
> ______________________________________________
> mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code. mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue 13 May 2008 - 19:30:33 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 13 May 2008 - 20:31:57 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive