[R] get: problem with environments
R. Michael Weylandt
michael.weylandt at gmail.com
Thu Oct 11 00:18:56 CEST 2012
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
More information about the R-help
mailing list