[R] how to access an executing environment?

Greg Snow 538280 at gmail.com
Thu May 29 16:40:52 CEST 2014


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.

Maybe if you tell us more about what you are trying to accomplish we
can give better suggestions for how to approach it.

On Wed, May 28, 2014 at 6:29 PM, Spencer Graves
<spencer.graves at 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 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.



-- 
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com



More information about the R-help mailing list