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.
Actually I need to provide get() access to the calling environment of sapply().
According to the documentation of parent.frame(), "The parent frame of a function evaluation is the environment in which the function was called". I tried to use parent.frame() instead of sys.frame() above,
but the variable "a" is never found, regardless of what value I give to the parameter "n" of
parent.frame().
I have basically three questions:
1. Do You have an idea how I could implement the code
more stably, so that the variable "a" is always visible to get, regardless of whether testfun
is used alone by itself or called from within another function?
2. Why does the implementation with parent.frame() not work?
3. Why does the number of contexts in the call stack differ in R and in rkward? It seems that when R is called from within the Linux terminal the call stack contains 4 contexts more that it does when is called from rkward. This also points to the instability of the code which I would like to solve.
Any suggestions on any on the above questions will be greatly appreciated.
Best regards,
Martin
get: problem with environments
5 messages · Martin Ivanov, Rui Barradas, arun +1 more
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? Cheers, Michael
Hello, No, I can't confirm the behavior the op sees. I've tried it in an R terminal on ubuntu 12.04, rkward on ubuntu, and RGui on Windows 7. R version 2.15.1. Here's the rkward sessionInfo. sessionInfo() R version 2.15.1 (2012-06-22) Platform: x86_64-pc-linux-gnu (64-bit) locale: [1] LC_CTYPE=pt_PT.UTF-8 LC_NUMERIC=C [3] LC_TIME=pt_PT.UTF-8 LC_COLLATE=pt_PT.UTF-8 [5] LC_MONETARY=pt_PT.UTF-8 LC_MESSAGES=pt_PT.UTF-8 [7] LC_PAPER=pt_PT.UTF-8 LC_NAME=pt_PT.UTF-8 [9] LC_ADDRESS=pt_PT.UTF-8 LC_TELEPHONE=pt_PT.UTF-8 [11] LC_MEASUREMENT=pt_PT.UTF-8 LC_IDENTIFICATION=pt_PT.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] rkward_0.5.6 loaded via a namespace (and not attached): [1] tools_2.15.1 Rui Barradas Em 07-10-2012 10:34, R. Michael Weylandt escreveu:
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? Cheers, Michael
______________________________________________ R-help at 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.
Hi, I am getting the same error message as Michael got (R terminal (R 2.15) on Ubuntu 12.04). ? testfun(x=5) #Error in sys.frame(which = x) : not that many frames on the stack ?print(testfun(x=6)) #Error in sys.frame(which = x) : not that many frames on the stack A.K. ----- Original Message ----- From: R. Michael Weylandt <michael.weylandt at gmail.com> To: Martin Ivanov <tramni at abv.bg> Cc: r-help at r-project.org Sent: Sunday, October 7, 2012 5:34 AM Subject: Re: [R] get: problem with environments
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? Cheers, Michael ______________________________________________ R-help at 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.
On Sun, Oct 7, 2012 at 12:33 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote:
Hello, No, I can't confirm the behavior the op sees. I've tried it in an R terminal on ubuntu 12.04, rkward on ubuntu, and RGui on Windows 7. R version 2.15.1. Here's the rkward sessionInfo. sessionInfo() R version 2.15.1 (2012-06-22) Platform: x86_64-pc-linux-gnu (64-bit)
On Sun, Oct 7, 2012 at 1:28 PM, arun <smartpink111 at yahoo.com> wrote:
I am getting the same error message as Michael got (R terminal (R 2.15) on Ubuntu 12.04). testfun(x=5) #Error in sys.frame(which = x) : not that many frames on the stack print(testfun(x=6)) #Error in sys.frame(which = x) : not that many frames on the stack
From: R. Michael Weylandt <michael.weylandt at gmail.com>
To: Martin Ivanov <tramni at abv.bg> Cc: r-help at r-project.org Sent: Sunday, October 7, 2012 5:34 AM Subject: Re: [R] get: problem with environments 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
So this all suggests something very strange has happened to your setup, Martin. Does this persist after a new session (perhaps running as R --vanilla) and/or reinstall? To your original questions:
1. Do You have an idea how I could implement the code more stably, so that the variable "a" is always visible to get, regardless of whether testfun is used alone by itself or called from within another function?
Well, you can do it without get() and just trust in the scoping rules
to make it work. Depending on your application, this might simplify
the code nicely.
If you really do need to compute something on the fly, tricks like
eval(parse(text = "")), as.name(), call() etc might work, but they're
occasionally difficult to get right.
a <- 54
eval(as.name("a")) + 2
testfun2 <- function(n, letter) eval(as.name(letter)) + n
testfun2(3, "a") # 57
2. Why does the implementation with parent.frame() not work?
You didn't show us how you tried to use parent.frame()
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. Cheers, Michael