[R] get: problem with environments

R. Michael Weylandt michael.weylandt at gmail.com
Sun Oct 7 11:34:50 CEST 2012

On Sun, Oct 7, 2012 at 10:16 AM, Martin Ivanov <tramni at abv.bg> wrote:
>  Dear R users,
> I am running R-2.15.1 in Linux Slackware64-14.0. Here is my minimal working example:
> testfun <- function (x) {
>  a <- 0;
>  sapply(X="a", FUN=get, envir=sys.frame(which=x));
> }
> Inside R, that is R called from within a Linux terminal, the following code works:
> testfun(x=5)
> print(testfun(x=6))
> But within rkward the above code fails and the following works:
> testfun(x=1)
> print(testfun(x=2))
> As you can see, the number of contexts up the call stack that contain the variable "a"
> varies depending on the implementation. If I call testfun() from within print(), I have to go
> one context up the call stack than if I call testfun() alone by itself. This implies
> some inherent instability of my code.

Unlike you, I don't get testfun(x = 5) to work in a terminal emulator.
As expected, I only get x =1 and print(... x = 2) to work and I just
tried this with R 2.15.0 and R devel, both in Terminal and by way of
ESS.  It could be a buglet introduced later into the 2.15 branch, but
that seems unlikely. As expected,

> testfun(5)
 Error in sys.frame(which = x) : not that many frames on the stack

Can someone else confirm the behavior you see?


More information about the R-help mailing list