Re: R-beta: Re: S Compatibility

Bill Venables (wvenable@attunga.stats.adelaide.edu.au)
Wed, 30 Apr 1997 17:12:22 +0930


Date: Wed, 30 Apr 1997 17:12:22 +0930
Message-Id: <9704300742.AA17932@attunga.stats.adelaide.edu.au>
From: Bill Venables <wvenable@attunga.stats.adelaide.edu.au>
To: Ross Ihaka <ihaka@stat.auckland.ac.nz>
Subject: Re:  R-beta: Re: S Compatibility
In-Reply-To: <199704300124.NAA05847@stat1.stat.auckland.ac.nz>

Ross Ihaka writes:
 > Bill Venables writes:
 > 
 > > Are the scoping differences between R and S set out precisely and
 > > definitively somewhere?  This would be useful.
 > 
 > In the source code perhaps? :-)

I should have added `concisely' ...

 > You can find a pretty precise description in the article Robert and I
 > did in JCGS.  

May I suggest the reference be added to the FAQ if it is not
already there?  It may not actually be a frequently asked
question, but it should be.

 > 
 > One really useful thing you can do with this is to create functions
 > with "own" or static variables.  Consider
 > 
 > 	make.linear.fun <- function(a, b) {
 > 		function(x) a + b * x
 > 	}
 > 
 > 	g <- make.linear.fun(2,3)
 > 
 > After this, g is a function of a single variable "x", but the
 > the body of g has access to the values of "a" and "b" which
 > were in effect when the function was created.

Of course this is also possbile in S, but much less elegantly.

> make.linear.fun <- function(a, b) 
+	substitute(function(x) .a +.b * x, list(.a = a, .b = b))
> make.linear.fun(2,3)
function(x)
2 + 3 * x
> make.linear.fun(2,3) -> funny
> funny(1)
[1] 5

-- 
Bill Venables, Head, Dept of Statistics,    Tel.: +61 8 8303 5418
University of Adelaide,                     Fax.: +61 8 8303 3696
South AUSTRALIA.     5005.   Email: Bill.Venables@adelaide.edu.au
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=