[Rd] Feature request: names(someEnv) same as ls(someEnv)

Duncan Murdoch murdoch at stats.uwo.ca
Sun Oct 15 22:45:15 CEST 2006


On 10/15/2006 4:15 PM, Robert Gentleman wrote:
> 
> Duncan Murdoch wrote:
>> On 10/15/2006 2:48 PM, Seth Falcon wrote:
>>> Hi,
>>>
>>> I would be nice if names() returned the equivalent of ls() for
>>> environments.
>> Wouldn't that just confuse people into thinking that environments are 
>> vectors?  Wouldn't it then be reasonable to assume that 
>> env[[which(names(env) == "foo")]] would be a synonym for env$foo?
> 
>   absolutely not - environments can only be subscripted by name, not by 
> logicals or integer subscripts - so I hope that most users would figure 
> that one out
> 
> 
>> I don't see why this would be nice:  why not just use ls()?
> 
>    why? environments do get used, by many as vectors (well hash tables), 
> modulo the restrictions on subscripting and the analogy is quite useful 
> and should be encouraged IMHO.

I think there's an important difference between a hash table and a 
vector.  Order matters in a vector.  Right now, if "foo" is the third 
name, then x$foo is the third element, but that's not true in an 
environment, where "third element" just doesn't make sense.

It will also be strange that you can ask to see the names, but you can't 
set them (unless you also want to also extend "names<-" to work on 
environments).

If you do go ahead with this change, I'd suggest implementing it as a 
new method

  names.environment <- function(x) ls(x)

rather than doing it at the C level.  At least then it will be 
consistent with the man page.

Duncan Murdoch

> 
>   Robert
> 
>> Duncan Murdoch
>>
>>> --- a/src/main/attrib.c
>>> +++ b/src/main/attrib.c
>>> @@ -687,6 +687,8 @@ SEXP attribute_hidden do_names(SEXP call
>>>      s = CAR(args);
>>>      if (isVector(s) || isList(s) || isLanguage(s))
>>>         return getAttrib(s, R_NamesSymbol);
>>> +    if (isEnvironment(s))
>>> +        return R_lsInternal(s, 0);
>>>      return R_NilValue;
>>>  }
>>>
>>>
>>> + seth
>>>
>>> --
>>> Seth Falcon | Computational Biology | Fred Hutchinson Cancer Research Center
>>> http://bioconductor.org
>>>
>>> ______________________________________________
>>> R-devel at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>




More information about the R-devel mailing list