On 5/29/2014 7:40 AM, Greg Snow wrote:
I believe that what is happening is that you never run fun1, so no
environment for fun1 is ever created and therefore x1 is never defined
with its own environment. You grab the statement y <- x1 from the
body of fun1, but then tries to evaluate it in the current environment
(where it cannot find x1).
The reason you cannot find the executing environment for fun1 is
because it is never created.
Of course. Thanks.
Maybe if you tell us more about what you are trying to accomplish we
can give better suggestions for how to approach it.
I'm trying to create a system for animation to generate a series
of plots from a function that creates one plot. The current status of
this effort is "animate" in "Ecfun" available on R-Forge
[install.packages("Ecdat", repos="http://R-Forge.R-project.org") or "svn
checkout svn://r-forge.r-project.org/svnroot/ecdat/"].
Since my earlier question, I fixed that problem with " funList
<- as.list(fun)" and then passing funList to eval. Other problems
remained with "op <- par(mar=c(2,2,2,1)); on.exit(par(op)); plot(1)":
First, "op" did not get assigned where it could be found. When I wrote
code to do assignments manually, "on.exit" was executed before "plot".
See below. I can work around this also, but I hope this isn't followed
by yet another problem like this.
Suggestions?
Thanks,
Spencer
fun3 <- function(fun){
funList <- as.list(fun)
bo <- body(fun)
nbo <- length(bo)
for(ib in seq(2, length=nbo-1)){
bi <- bo[[ib]]
print(objects())
print(par('mar'))
if(as.character(bi[[1]])=="<-"){
if(length(bi)==3){
assign(as.character(bi[[2]]),
eval(bi[[3]], funList) )
next
} else {
warning('Possible eval problem in ',
as.character(bi))
}
}
eval(bi, funList)
}
print(objects())
}
funPar <- function(y1=1, y2=2){
op <-par(mar=c(2,2,2,1))
on.exit(par(op))
plot(y1)
}
fun3(funPar)
[1] "bi" "bo" "fun" "funList" "ib" "nbo"
[1] 5.1 4.1 4.1 2.1
[1] "bi" "bo" "fun" "funList" "ib" "nbo" "op"
[1] 2 2 2 1
[1] "bi" "bo" "fun" "funList" "ib" "nbo" "op"
[1] 5.1 4.1 4.1 2.1
[1] "bi" "bo" "fun" "funList" "ib" "nbo" "op"
On Wed, May 28, 2014 at 6:29 PM, Spencer Graves
<spencer.gra...@structuremonitoring.com> wrote:
Hello:
I'm writing code to modify a function, and I want to know how to
access the executing environment of the function. The example below
extracts the body of a function and executes a single line but can't find
"x1" in the function's executing environment. How would you suggest fixing
this? Thanks, Spencer
fun1 <- function(x1=1){
y <- x1
}
fun2 <- function(fun=fun1){
bo <- body(fun)
bo2 <- bo[[2]]
z <- eval(bo2)
}
tst <- fun2()
Error in eval(expr, envir, enclos) : object 'x1' not found
______________________________________________
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.
______________________________________________
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.