[R] Using variables from different environments in one function

Duncan Murdoch murdoch.duncan at gmail.com
Sat Oct 6 01:37:12 CEST 2012


On 12-10-05 3:47 PM, Rik Verdonck wrote:
> Dear R-community,
>
>
> I have been experiencing this issue with functions within a function and
> I can kind of feel where the problem is (environments, closures etc),
> but I don't manage to solve it.
>
> I will sketch it with an example:
>
> #We have two simple "inner" functions:
>
> ############## INNER FUNCTION1 #################
> innerfunction1<-function()
> {
>          ab<-a+b
>          cd<-c+d
>          innerfunctionlist<-list("ab"=ab,"cd"=cd)
> }
> ################################################
>
> ############## INNER FUNCTION2 #################
> innerfunction2<-function()
> {
>          print(AB+CD)
> }
> ################################################
>
> #And we have a main script with a number of predefined variables.
> #This shapes an environment in which variables that will be used
> #by the inner function are defined.
> #############   MAIN SCRIPT     ################
>          a=1
>          b=2
>          c=3
>          d=4
>
>
>          #source(filepath/to/innerfunction1)
>          outcome1<-innerfunction1()
>
>          AB      <-outcome1$ab
>          CD      <-outcome1$cd
>
>          #source(filepath/to/innerfunction2)
>          innerfunction2()
>
>
> #################################################
>
>
>
> #So far so good. No problem if you run this.
> #The problem emerges if you want to make the main script a function.
> #So now I wrap it into a function:
>
>
>          main<-function()
>          {
>                  a=1
>                  b=2
>                  c=3
>                  d=4
>
>                  #source(filepath/to/innerfunction1)
>                  outcome1<-innerfunction1()
>
>                  AB <-outcome1$ab
>                  CD <-outcome1$cd
>
>                  #source(filepath/to/innerfunction2)
>                  innerfunction2()
>          }
>
>
>
> when I now run main() in an environment where all these variables are not defined, I will get this error
> message:
>
> Error in innerfunction1() : object 'a' not found
>
> I can solve this by defining the variables a, b, c and d. However, I
> will then get next error message:
>
> Error in print(AB + CD) : object 'AB' not found
>
>
> I think I see what happens. The right variables are not present
> in the right environment. Probably I did something silly, but I really
> don't see how to solve this. Can someone please help me?

Simply define the "inner" functions within the main function.  They'll 
have access to all of its local variables, just as they currently have 
access to all the variables in the global environment where they are 
defined in the current version.

Duncan Murdoch

>
> Many thanks!
> Rik Verdonck
> University of Leuven
>
> 	[[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