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:
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
______________________________________________
R-help@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.