[R] scope of variables in R
Wacek Kusnierczyk
Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Tue Mar 31 22:28:59 CEST 2009
Wacek Kusnierczyk wrote:
>
> this is where multiple assignment might be your friend:
>
> source('http://miscell.googlecode.com/svn/rvalues/rvalues.r')
> c(foo, bar, gee) := local({
> shared = 0
> foo = function() shared <<- 0
> bar = function(bar) shared <<- bar
> gee = function() print(shared)
> c(foo, bar, gee) })
>
... and of course, the functions do not need to be named within local,
unless recursion, for example, is used:
source('http://miscell.googlecode.com/svn/rvalues/rvalues.r')
c(reset, set, get) := local({
shared = 0
c( function() shared <<- 0,
function(bar) shared <<- bar,
function() print(shared) ) })
the ugly <<- is used here, but always with reference to a variable found
in the enclosing local scope, so the risk of messing with other
variables in outer scopes is minimal.
vQ
More information about the R-help
mailing list