Skip to content
Prev 325259 / 398503 Next

Functions within functions - environments

Hello,

See the help page for parent.frame, and use its argument to go back to 
the frames of variables 'a' and 'b':


innerfunction<-function()
{
     env1 <- parent.frame(1)  # for 'b'
     env2 <- parent.frame(2)  # for 'a'
     print(paste(env2$a, " from inner function"))
     print(paste(env1$b, " from inner function"))
     setwd(wd)
}


Now it complains about 'wd'.
(Are you sure you want to hard code the number of frames to go back to? 
It seems better to write "normal" functions, i.e., functions with 
arguments. innerfunction would have 2 args and middlefunction just one, 
'a'.)

Hope this helps,

Rui Barradas

Em 12-06-2013 18:42, Rik Verdonck escreveu: