Re: [R] 'autocompletion' of named elements of a list

From: Peter Ehlers <ehlers_at_ucalgary.ca>
Date: Fri, 11 Mar 2011 07:04:48 -0800

On 2011-03-11 01:07, Ivan Calandra wrote:
> Hi,
>
> From ?"$", you can see that using [[ instead would do what you're
> looking for. You should read and try to understand the whole help file.
> The reason is that for [[ the default is exact=TRUE, wheareas for $ the
> only possible value is exact=FALSE, which means partial matching if
> possible.
>
> So try A[["aa"]] instead.

Indeed. And, as always, shortcuts come with caveats. Like using T/F for TRUE/FALSE, use of $ extraction may be convenient but is best avoided unless you're sure of its effect.

Peter Ehlers

>
> HTH,
> Ivan
>
> Le 3/11/2011 09:55, Håvard Rue a écrit :
>> I had an obscure bug that boiled down to this ``feature'' in R,
>>
>> Read 3921 items
>>> A = list(aa = 1)
>>> A
>> $aa
>> [1] 1
>>
>>> if (A$a) print("a is there")
>> [1] "a is there"
>>
>> The test appear to check is A$a is TRUE, but what happen is that it
>> auto-complete (silently), and expand to 'A$aa'.
>>
>> The problem was caused by the fact that I had also a element named 'a'
>> which was evaluated to NULL, like
>>
>>> A$a = NULL
>>> A
>> $aa
>> [1] 1
>>
>>> if (A$a) print("a is there")
>> [1] "a is there"
>>
>> since NULL elements are removed from the list, and that A$a auto-expand
>> to A$aa, my error appeared.
>>
>> To me, this seems not a feature I want in order to have a robust
>> program.
>>
>> Is there any option to turn this ''feature'' off?
>>
>> Best,
>> H
>>
>>
>>
>



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. Received on Fri 11 Mar 2011 - 15:12:22 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 11 Mar 2011 - 16:00:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive