[R] Functions within functions - environments

William Dunlap wdunlap at tibco.com
Wed Jun 12 20:09:28 CEST 2013


A function looks up free variables in the environment in which the function
was defined, not in the environment in which the function was called.  The
latter is called something like 'dynamic scoping' and usually leads to trouble,
the former is 'lexical scoping' and leads to predictable results.  You can do
dynamic scoping in R (using get(), assign(), and parent.frame()), but it
leads to code that only works correctly in favorable circumstances.  You should pass
data from the caller to the callee via the argument list, not via free variables.

To use lexical scoping define your functions as:

outerfunction<-function()
{
     middlefunction<-function()
     {
          innerfunction<-function()
          {
              print(paste(a, " from inner function"))
              print(paste(b, " from inner function"))
              # setwd(wd) # wd was not defined in your mail
        }
        b="b"
        print(paste(b, " from middle function"))
        innerfunction()
     }
     a="a"
     print(paste(a," from outer function"))
     middlefunction()
}

> outerfunction()
[1] "a  from outer function"
[1] "b  from middle function"
[1] "a  from inner function"
[1] "b  from inner function"

(One can change the environment of a function after it is defined, but
there is not often a need to do that.)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Rik Verdonck
> Sent: Wednesday, June 12, 2013 10:42 AM
> To: r-help at r-project.org
> Subject: [R] Functions within functions - environments
> 
> Dear list,
> 
> 
> I have a problem with nested functions and I don't manage to get it
> solved. I know I should be looking in environments, and I have tried a
> lot, but it keeps on erroring.
> An easy version of the problem is as follows:
> 
> 
> 
> innerfunction<-function()
> {
>     print(paste(a, " from inner function"))
>     print(paste(b, " from inner function"))
>     setwd(wd)
> }
> 
> 
> middlefunction<-function()
> {
>     b="b"
>     print(paste(b, " from middle function"))
>     innerfunction()
> }
> 
> 
> outerfunction<-function()
> {
>     a="a"
>     print(paste(a," from outer function"))
>     middlefunction()
> }
> 
> outerfunction()
> 
> 
> 
> Here, R will tell me that the inner function has no clue what a or b is.
> So what I want, is the inner function to recognize a and b.
> Any suggestions?
> 
> Many thanks!
> Rik
> 
> 
> 
> 
> --
> ________________________________________________
> 
> Rik Verdonck
> 
> 
> 
> Research group of Molecular Developmental Physiology and Signal
> Transduction
> KULeuven, Faculty of Science
> Naamsestraat 59,  Bus 02465
> B-3000 Leuven,Belgium
> Tel. +32 16 32 39 65
> Fax +32 16 32 39 02
> 
> http://bio.kuleuven.be/df/JV/index2.htm
> rik.verdonck at bio.kuleuven.be
> 
> 
> 
> 
> 
>       Please consider the environment before printing this e-mail
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list