[R] Problems accessing environment() in function

Heiko Neuhaus webmaster at euros4click.de
Wed May 2 23:20:59 CEST 2012


Thank you very much for your suggestion.

> 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
> }

I have already figured out a very similar solution using for/eval that 
basically does the same. I was hoping that I would somehow could get 
around the ugly loop using a more elegant approach. The loop should have 
a negative impact on performance since my function is using a lot of 
parameters. I was hoping, that there was some flag to tell the as.list 
function to ignore non existing objects. If that is not possible I will 
have to accept this.

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

Of course. That is why I call it at the very first line of my function.

Thanks again!

Heiko Neuhaus



More information about the R-help mailing list