[R] eval and evironments: call local function in a global function

Gabor Grothendieck ggrothendieck at gmail.com
Fri Aug 21 18:17:09 CEST 2009


Just one correction. The line environment(fun.global) <- environment()
line below should be deleted. It was needed in a prior example but in this one
we use parent.frame()$setVar(4) in the call so that line is not needed in main.

On Fri, Aug 21, 2009 at 7:09 AM, Gabor
Grothendieck<ggrothendieck at gmail.com> wrote:
> Yet another possibility is to simply mandate that the user call setVar
> directly from fun.global and ask them to do it like this:
>
> fun.global <- function() parent.frame()$setVar(4)
> main <- function() {
>       l.var <- 0
>       setVar <- function(value) { message("set Var"); l.var <<- value }
>       environment(fun.global) <- environment()
>       fun.global()
>       print(l.var)
> }
> main()




More information about the R-help mailing list