Skip to content
Prev 293344 / 398502 Next

Problems accessing environment() in function

On 02/05/2012 12:59 PM, Heiko Neuhaus wrote:
That's hard to do, because missing arguments exist, they just have a 
special value to signal that they were missing.  The missing() function 
tests for that value, but it is picky about its arguments.  So if you 
want to do all of this in R you probably need some tricky programming, 
like this:

f <- function(a,b,c) {
   names <- ls(environment())  # get all the names
   result <- list()
   for (n in names) {
     if (!do.call(missing, list(as.name(n))))
       result[n] <- get(n)
   }
   result
}

If you put the ls() call later, you'll pick up other local variables 
(names, result, n) as well.

Duncan Murdoch