我正在尝试创建一个函数,其中创建了一个命名列表(我需要使用这个特定的结构,因为它需要调用下游函数).但是,尽管名称被定义为函数的参数,但它没有被执行.这是一个最小的例子:
make_list = function(first, second){
return(list(first=second))
}
make_list("name", "value")
#$`first`
#[1] "value"
Run Code Online (Sandbox Code Playgroud)
注意名称"first",而不是"name".第一个意图只是函数中的一个参数,但它没有被这样使用.任何建议都非常感谢.
在声明中list(first=second)," first"是属性名称而不是变量first.
make_list = function(first, second){
ret = list()
ret[[first]] = second
return(ret)
}
make_list("name", "value")
#$name
#[1] "value"
Run Code Online (Sandbox Code Playgroud)