[R] Local Scopes in S

Douglas Bates bates at stat.wisc.edu
Fri Oct 20 20:14:25 CEST 2000


Thomas Lumley <thomas at biostat.washington.edu> writes:

> On Fri, 20 Oct 2000, Jorge Luis Ojeda Cabrera wrote:
> 
> > 
> > I 've been working with R since last year and I am quite "happy" with R 
> > capabilities. Nevertheless, I need to present my job to other people 
> > (S-users) and I am facing a problem with R & S compatibility: my 
> > R-scripts make intensive use of functions local scope, and as far as I 
> > know(I am a new to S) S does not have this capability.
> > 
> > I use to  do things like:
> > 
> > create.f <- function(data)
> > {
> > 	f <- function(x) { return( sum(data-x) );}
> > 	return( f )
> > }
> > 
> > d <- 1:10
> > g <- create.f(d)
> > d <- NULL
> > g(1)
> > [1] 45
> > 
> > Does exist an easy way to solve the problem ?
> > May be I've missed some easier implementation in R..?
> 
> This sort of R function can be ported to S using the MC() (make
> closure) function by Luke Tierney that appears in the FAQ (section 3.3.1).

As a general answer I would advise referring to Venables and Ripley's
"S Programming" (Springer, 2000) for porting questions like this.
They explain the differences between the two dialects of the S
language with regard to scoping.  You may find that you want to use
assignments into frame 1 for the S version.

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list