[prev in list] [next in list] [prev in thread] [next in thread] 

List:       r-help
Subject:    Re: [R] Random number generation
From:       Duncan Murdoch <murdoch () stats ! uwo ! ca>
Date:       2008-05-13 17:58:43
Message-ID: 4829D6D3.8020704 () stats ! uwo ! ca
[Download RAW message or body]

On 5/13/2008 1:38 PM, Greg Snow wrote:
> > -----Original Message-----
> > From: Esmail Bonakdarian [mailto:esmail.js@gmail.com]
> > Sent: Tuesday, May 13, 2008 8:13 AM
> > To: Greg Snow
> > Cc: Prof Brian Ripley; r-help@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@imail.org
> (801) 408-8111
> 
> ______________________________________________
> R-help@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.

______________________________________________
R-help@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.


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic