[R] Using variables from different environments in one function

Ista Zahn istazahn at gmail.com
Fri Oct 5 23:20:30 CEST 2012


Hi Rik,

I don't fully understand it, but setting local=TRUE to your source()
calls will avoid the specific error you noted.

Best,
Ista

On Fri, Oct 5, 2012 at 3:47 PM, Rik Verdonck
<Rik.Verdonck at bio.kuleuven.be> 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?
>
> 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