[R] Partial matching list elements in R 4.0

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Tue May 26 22:48:03 CEST 2020


Hmmm... yes. I read (past tense) that passage as meaning that **when
extracting** partial matching is only done with $. I did not read  it as
also saying that with assignment, partial matching with $ is not done, but
I see how you could. As Bill D.'s example showed, even R seems confused
about how $ should behave on assignment. As I said, best to avoid the issue
altogether by using [[ ]], where matching behavior can be explicitly
controlled and the default is "exact, "which has been recommended here from
time to time by others also. I grant you that such ambiguity is not
desirable, though.

Bert




Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Tue, May 26, 2020 at 12:54 PM John Harrold <john.m.harrold using gmail.com>
wrote:

> Hello Bert,
>
> I've read the documentation and I didn't think it applied here. Perhaps
> it's my reading of that documentation I'm confused by. I stays *only when
> extracting*. What I'm doing here is assigning values. Is the expected
> behavior really to create a copy of the "misc" element in "mi" and then set
> all of the values in "misc" to NULL?
>
> Thanks,
> John
>
> On Tue, May 26, 2020 at 11:32 AM Bert Gunter <bgunter.4567 using gmail.com>
> wrote:
>
>> I can't answer your question (about your R programming skills) but the
>> behavior you complain about is as documented. In particular:
>>
>> "Thus the default behaviour is to use partial matching only when
>> extracting from recursive objects (except environments) by $. Even in
>> that case, warnings can be switched on by options
>> <http://127.0.0.1:39592/help/library/base/help/options>(warnPartialMatchDollar
>> = TRUE)."
>>
>> So the solution is not to use $ for list extraction/replacement. Though
>> convenient, it is prone to such issues. Instead, the following works (as
>> does your suggested solution, of course):
>>
>> > var <- list()
>> > var[["options"]][["misc"]][["abc"]] <- "123"
>> > var[["options"]][["mi"]][["something"]] <- 13
>> > var
>> $options
>> $options$misc
>> $options$misc$abc
>> [1] "123"
>>
>>
>> $options$mi
>> $options$mi$something
>> [1] 13
>>
>> Bert Gunter
>>
>> "The trouble with having an open mind is that people keep coming along
>> and sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>
>>
>> On Tue, May 26, 2020 at 10:45 AM John Harrold <john.m.harrold using gmail.com>
>> wrote:
>>
>>> Hello,
>>>
>>>
>>> I'm testing some code in R 4.0, and I'm having an issue with the
>>> following"
>>>
>>> # -------------
>>> rm(list=ls())
>>> graphics.off()
>>> #load("/tmp/post.RData")
>>> var = list();
>>> # If I uncomment this it fixes things:
>>> # var$options = list(mi   = list(),
>>> #                    misc = list())
>>> #
>>> var$options$misc$abc = "123"
>>> var$options$mi$something    = 13
>>> #------------
>>>
>>> This is a stripped down example but it exhibits the issue I"m having.
>>> Basically when I create the list element var$options$mi the contents of
>>> var$options$misc move over to var$options$mi. And what was in
>>> var$options$misc become NULL:
>>>
>>> So now var$options looks like:
>>>
>>> var$options
>>> $misc
>>> $misc$abc
>>> NULL
>>>
>>> $mi
>>> $mi$abc
>>> [1] "123"
>>> $mi$something
>>> [1] 13
>>>
>>> This worked (still works) in R 3.5.1. I understand partial matching, but
>>> is
>>> this normal lists moving over to elements like this? I can uncomment the
>>> text mentioned in the example and it seems to fix it, but I'm wondering
>>> if
>>> this is a bug or just my poor programming coming back to bite me.
>>>
>>> I've included my sessionInfo() at the bottom.
>>>
>>> Thanks
>>> John
>>> :wq
>>>
>>>
>>> sessionInfo()
>>>
>>> R version 4.0.0 (2020-04-24)
>>>
>>> Platform: x86_64-apple-darwin17.0 (64-bit)
>>>
>>> Running under: macOS Mojave 10.14.5
>>>
>>>
>>> Matrix products: default
>>>
>>> BLAS:
>>> /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
>>>
>>> LAPACK:
>>>
>>> /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
>>>
>>>
>>> locale:
>>>
>>> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>>>
>>>
>>> attached base packages:
>>>
>>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>>
>>>
>>> other attached packages:
>>>
>>> [1] gdata_2.18.0  ggplot2_3.3.0 deSolve_1.28
>>>
>>>
>>> loaded via a namespace (and not attached):
>>>
>>>  [1] Rcpp_1.0.4.6     gtools_3.8.2     withr_2.2.0      assertthat_0.2.1
>>>
>>>  [5] dplyr_0.8.5      digest_0.6.25    crayon_1.3.4     grid_4.0.0
>>>
>>>  [9] R6_2.4.1         lifecycle_0.2.0  gtable_0.3.0     magrittr_1.5
>>>
>>> [13] scales_1.1.1     pillar_1.4.4     rlang_0.4.6      vctrs_0.3.0
>>>
>>> [17] ellipsis_0.3.1   glue_1.4.1       purrr_0.3.4      munsell_0.5.0
>>>
>>> [21] compiler_4.0.0   pkgconfig_2.0.3  colorspace_1.4-1 tidyselect_1.1.0
>>>
>>> [25] tibble_3.0.1
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
>>>
>>
>
> --
> John
> :wq
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list