[R] Problems accessing environment() in function

Uwe Ligges ligges at statistik.tu-dortmund.de
Wed May 2 18:36:41 CEST 2012



On 01.05.2012 19:57, Heiko Neuhaus wrote:
> Hi all,
>
> I am trying to create a list of all variable/value combinations in
> environment().
>
> When a function with unset arguments is called, the method I have been
> using fails with a "missing argument" error. However it should be
> possible to simply skip these missing objects in the generation of the
> list?
>
> Could anyone recommend me a better way (that does not use a slow
> for/eval-combination) to achieve the desired effect?
>
> You can easily reproduce my problem using this code:
>
> ------------------------------
>
> test1 <- function(a, b, c)
> {
> x <- as.list(environment())
> print ("hi from test1!")
> test2(a = a, b = b, c = c)

You are rying to pass a, b, c here and hence R tries to insert those 
into the environment of test2 once it is called, you have not passed 
arguments to your test1 call.

Uwe Ligges

> }
>
> test2 <- function(a, b, c)
> {
> # PROBLEM: Why can't I get a list as in test1() here?
> x <- as.list(environment())
> print ("hi from test2!")
> }
>
> test1()
>
> ------------------------------
>
> I want my list "x" in test2() to work behave just like "x" in test1().
> (In this example the correct list in test2() would be empty).
>
> Thanks a lot for your time.
>
> Best wishes,
> Heiko Neuhaus
>
> ______________________________________________
> 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.



More information about the R-help mailing list