I'm trying to rename a list object where the object name is being put together with either str_c() or paste(), but can't seem to get it to work. I'd assume this is possible, but I can't figure out how.
# some data
params <- tibble(col1=c(1,2,3), col2=c("a", "b", "c"))
# create empty list
all_output <- list()
# append new df to list
all_output <- append(all_output, list("this_works_fine" = params))
# append new df to list using the concatenated string
all_output <- append(all_output, list(str_c("this_", "does_", "not_", "work") = params))
Thanks for your help!
CodePudding user response:
Use either setNames
or
append(all_output, setNames(list(params), str_c("this_", "does_", "not_", "work")))
-output
$this_works_fine
# A tibble: 3 × 2
col1 col2
<dbl> <chr>
1 1 a
2 2 b
3 3 c
$this_does_not_work
# A tibble: 3 × 2
col1 col2
<dbl> <chr>
1 1 a
2 2 b
3 3 c
or use lst
from dplyr
with :=
append(all_output, lst(!!str_c("this_", "does_", "not_", "work") := params))
-output
$this_works_fine
# A tibble: 3 × 2
col1 col2
<dbl> <chr>
1 1 a
2 2 b
3 3 c
$this_does_not_work
# A tibble: 3 × 2
col1 col2
<dbl> <chr>
1 1 a
2 2 b
3 3 c