There is a modifyList function in pkg utils that is used extensively in the code for lattice graphics:

 var$options <- modifyList(var$options, list( misc=list(abc = "123"), mi= list(something    = 13))
                                             )

#-------------------

> var
$options
$options$mi
$options$mi$something
[1] 13


$options$misc
$options$misc$abc
[1] "123"

On 5/26/20 11:31 AM, Bert Gunter 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.harr...@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@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.

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

______________________________________________
R-help@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.

Reply via email to