R-beta: Re: S Compatibility

Bill Venables wvenable at attunga.stats.adelaide.edu.au
Wed Apr 30 09:42:22 CEST 1997


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 at 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 at stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=



More information about the R-help mailing list