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) } 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
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.
Thanks a lot for your answer!>> ------------------------------ >> >> 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 LiggesI am aware that I am passing non existing arguments here, which is why my method of creating a list of the environment "as.list(environment())" seems to fail in this case. What I need is a way to just skip non existing objects when I create my list. In my given example I was intending to receive an empty list, since no valid arguments were passed to test2(). In other words: I want a list containing all _existing_ variable/value combinations and just skip the missing ones. Thanks again for your time Best wishes, Heiko Neuhaus