What I try to do is to set the name of a list element dynamically in R, from a predefined string and using non-standard evaluation.
See the example below:
library(rlang)
dynamic.listname <- "important.name"
# this works (as was also demonstrated in the answer to this related question: https://stackoverflow.com/questions/35034384/dynamically-creating-named-list-in-r ):
list.to.display <- list(1,2,3)
names(list.to.display) <- c("first.fixed.name", dynamic.list.name, "second.fixed.name")
# But I would like something like this to work
list.to.display <- list(
"first.fixed.name" = 1,
!!dynamic.listname := 2,
"second.fixed.name" = 3
)
# it gives the following error:
# Error: `:=` can only be used within a quasiquoted argument
I based the code above on a tidyverse example, the last paragraph named "Setting variable names" on this website: https://dplyr.tidyverse.org/articles/programming.html
Thus, 'dynamic.listname' should be evaluated first as the string that was stored inside that variable. Then, this string should be implemented as one of the names in the list. Anyone knows how to implement non-standard evaluation in this situation? Base R is also fine for me. The reason I would like to the other options that is currently not yet working is that the list I want to use is embedded in a specific package function and thus a bit hard to manipulate from the outside. In addition, I am trying to learn more about non-standard evaluation.