list_named.RdA version of list(...), but with “automatically”
named list components.
list_(...)The names are extracted from sys.call(), and the function
is written to be fast (rather than easy to ready for the uninitiated ;-)
a <- 1:4; lett <- letters[1:9]; CH <- "Suisse"
all.equal(list (a, lett),
list_(a, lett)) # "names for current but not for target"
#> [1] "names for current but not for target"
str(list(a, lett, CH)) # [[1]], [[2]], .. (no names)
#> List of 3
#> $ : int [1:4] 1 2 3 4
#> $ : chr [1:9] "a" "b" "c" "d" ...
#> $ : chr "Suisse"
str(list_(a, lett, CH))# $a $lett ..
#> List of 3
#> $ a : int [1:4] 1 2 3 4
#> $ lett: chr [1:9] "a" "b" "c" "d" ...
#> $ CH : chr "Suisse"
stopifnot(identical( list (a, lett, CH),
unname(L <- list_(a, lett, CH))),
is.list(L), names(L) == c("a", "lett", "CH"),
identical(lett, L$lett) ## etc
)
## The function is currently defined as
function (...) `names<-`(list(...), vapply(sys.call()[-1L], as.character, ""))
#> function (...)
#> `names<-`(list(...), vapply(sys.call()[-1L], as.character, ""))
#> <environment: 0x55dd15703ef0>