我尝试做的是在 R 中动态设置列表元素的名称,从预定义的字符串并使用非标准评估。
请参见下面的示例:
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
我将上面的代码基于一个 tidyverse 示例,该网站上最后一段名为“设置变量名称”:https ://dplyr.tidyverse.org/articles/programming.html
因此,应该首先将“dynamic.listname”作为存储在该变量中的字符串进行评估。然后,这个字符串应该被实现为列表中的名称之一。任何人都知道如何在这种情况下实施非标准评估?Base R 也适合我。我想要目前尚未使用的其他选项的原因是我要使用的列表嵌入在特定的包函数中,因此从外部操作有点困难。此外,我正在尝试了解更多关于非标准评估的信息。