Thank You very much for Your replies.
Dear Michael,
"Does this persist after a new session (perhaps running as R --vanilla) and/or reinstall?"
Yes, it does. After running R --vanilla, still there are 4 contexts more on the call stack.
"You didn't show us how you tried to use parent.frame()"
I did it like this:
testfun1 <- function (x1) {
a1 <- 1;
sapply(X="a1", FUN=get, envir=parent.frame(x1));
}
testfun1(x1=1);
The above code never succeeds no matter what a number I give to x1.
" 3. Why does the number of contexts in the call stack differ in R and in rkward?
It shouldn't. This is an issue that needs further sorting out."
Here is some more info on my setup:
sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-slackware-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US LC_NUMERIC=C LC_TIME=en_US
[4] LC_COLLATE=C LC_MONETARY=en_US LC_MESSAGES=en_US
[7] LC_PAPER=C LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_US LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_2.15.1
Best regards,
Martin
get: problem with environments
5 messages · Martin Ivanov, Thomas Lumley, R. Michael Weylandt
3 days later
Can someone more capable than I help Martin out with this? I'm feeling out of my league (that or I've missed something obvious) Shot in the dark: you aren't running this in some sort of debug mode, are you? RMW
On Sun, Oct 7, 2012 at 5:10 PM, Martin Ivanov <tramni at abv.bg> wrote:
Thank You very much for Your replies.
Dear Michael,
"Does this persist after a new session (perhaps running as R --vanilla) and/or reinstall?"
Yes, it does. After running R --vanilla, still there are 4 contexts more on the call stack.
"You didn't show us how you tried to use parent.frame()"
I did it like this:
testfun1 <- function (x1) {
a1 <- 1;
sapply(X="a1", FUN=get, envir=parent.frame(x1));
}
testfun1(x1=1);
The above code never succeeds no matter what a number I give to x1.
" 3. Why does the number of contexts in the call stack differ in R and in rkward?
It shouldn't. This is an issue that needs further sorting out."
Here is some more info on my setup:
sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-slackware-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US LC_NUMERIC=C LC_TIME=en_US
[4] LC_COLLATE=C LC_MONETARY=en_US LC_MESSAGES=en_US
[7] LC_PAPER=C LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_US LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_2.15.1
Best regards,
Martin
On Thu, Oct 11, 2012 at 9:05 AM, R. Michael Weylandt
<michael.weylandt at gmail.com> wrote:
Can someone more capable than I help Martin out with this? I'm feeling out of my league (that or I've missed something obvious) Shot in the dark: you aren't running this in some sort of debug mode, are you? RMW On Sun, Oct 7, 2012 at 5:10 PM, Martin Ivanov <tramni at abv.bg> wrote:
Thank You very much for Your replies.
Dear Michael,
"Does this persist after a new session (perhaps running as R --vanilla) and/or reinstall?"
Yes, it does. After running R --vanilla, still there are 4 contexts more on the call stack.
"You didn't show us how you tried to use parent.frame()"
I did it like this:
testfun1 <- function (x1) {
a1 <- 1;
sapply(X="a1", FUN=get, envir=parent.frame(x1));
}
testfun1(x1=1);
The above code never succeeds no matter what a number I give to x1.
This one is straightforward.
The smallest value you can give to parent.frame() is 1, which in this
case is the frame from which testfun1 is called. Compare these two
testfun1 <- function (x1) {
a1 <- 1
sapply(X="a1", FUN=get, envir=parent.frame(x1)))
}
testfun1(x1=1);
testfun2 <- function (x1) {
a1 <- 1
sapply(X="a1", FUN=function(x) get(x, envir=parent.frame(x1)))
}
testfun2(x1=1);
testfun1() never finds a1==1, but testfun2(3) does.
Remember, actual arguments to sapply() will be evaluated in the frame
sapply() is called from. It's only default arguments that are
evaluated inside the function.
Thomas Lumley Professor of Biostatistics University of Auckland
On Wed, Oct 10, 2012 at 10:04 PM, Thomas Lumley <tlumley at uw.edu> wrote:
On Thu, Oct 11, 2012 at 9:05 AM, R. Michael Weylandt <michael.weylandt at gmail.com> wrote:
Can someone more capable than I help Martin out with this? I'm feeling out of my league (that or I've missed something obvious) Shot in the dark: you aren't running this in some sort of debug mode, are you? RMW On Sun, Oct 7, 2012 at 5:10 PM, Martin Ivanov <tramni at abv.bg> wrote:
Thank You very much for Your replies.
Dear Michael,
"Does this persist after a new session (perhaps running as R --vanilla) and/or reinstall?"
Yes, it does. After running R --vanilla, still there are 4 contexts more on the call stack.
"You didn't show us how you tried to use parent.frame()"
I did it like this:
testfun1 <- function (x1) {
a1 <- 1;
sapply(X="a1", FUN=get, envir=parent.frame(x1));
}
testfun1(x1=1);
The above code never succeeds no matter what a number I give to x1.
This one is straightforward.
The smallest value you can give to parent.frame() is 1, which in this
case is the frame from which testfun1 is called. Compare these two
testfun1 <- function (x1) {
a1 <- 1
sapply(X="a1", FUN=get, envir=parent.frame(x1)))
}
testfun1(x1=1);
testfun2 <- function (x1) {
a1 <- 1
sapply(X="a1", FUN=function(x) get(x, envir=parent.frame(x1)))
}
testfun2(x1=1);
testfun1() never finds a1==1, but testfun2(3) does.
Remember, actual arguments to sapply() will be evaluated in the frame
sapply() is called from. It's only default arguments that are
evaluated inside the function.
Thanks Prof Lumley, I'm still not sure how this gets to the call stack of 5 or 6 the OP reported or the difference between GUI & Terminal. Any thoughts there? Michael
On Thu, Oct 11, 2012 at 11:18 AM, R. Michael Weylandt
<michael.weylandt at gmail.com> wrote:
Thanks Prof Lumley, I'm still not sure how this gets to the call stack of 5 or 6 the OP reported or the difference between GUI & Terminal. Any thoughts there?
I don't see how the terminal version is getting 5 and 6 rather than 1 and 2, but my testfun2() lets the OP do what he was originally trying to do. The difference between testfun() and print(testfun()) (ie, 1 vs 2, or 5 vs 6) is because of lazy evaluation: in print(testfun()), testfun() is called from inside print() when it needs the value to do method dispatch. I can't reproduce the value of 5, so I can't help much. For anyone wanting to experiment further, it would probably be simpler to use testfun<-function() sys.nframe() rather than looking at whether a variable is found or not. I can't see how testfun() typed at the global command prompt can return anything other than 1, but maybe something is getting in between the console and the evaluator. For example:
testfun()
[1] 1
print(testfun())
[1] 2
capture.output(testfun())
[1] "[1] 6" I don't see why a pure console program should do this on Linux, though. -thomas
Thomas Lumley Professor of Biostatistics University of Auckland