[R] Problems accessing environment() in function

Duncan Murdoch murdoch.duncan at gmail.com
Thu May 3 00:49:43 CEST 2012


On 12-05-02 5:20 PM, Heiko Neuhaus wrote:
> 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.

What you're doing looks ugly, so don't expect an elegant solution.  (I 
say it's ugly because you're redefining terms like "exists".)  If you 
work with the language things will look nicer.

Duncan Murdoch

  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.



More information about the R-help mailing list