它再次发生了。我刚要按下发布答案按钮的问题已被删除:
我正在寻找一种方法,从函数的省略号参数中提取绑定到符号的对象的值,以及符号。也就是说,我试图以引用透明的方式从省略号中提取符号。我试过玩替补和lazy_dots并没有取得成功。
# function I'm trying to create
get_symbols <- function(...) {
# insert code here
# should return a character vector of symbols
}
# example function calls
some_function <- function(...){
get_symbols(...)
}
z <- quote(T)
some_function(quote(x), ~y, z)
# should return vector of symbols c(x, ~y, T)
some_function(quote(x), ~y, quote(T))
# should return vector of symbols c(x, ~y, T)
答案 0 :(得分:4)
查看substitute
和deparse
的这些用途是否有用。 deparse
函数从(有效)语言表达式返回文本/字符向量。 :
some_function1 <- function(...){
print(sapply( substitute(list(...)), deparse)[-1]) # -1 drops "list"
}
z <- quote(T)
some_function1(quote(x), ~y, z)
#[1] "quote(x)" "~y" "z"
some_function1(quote(x), ~y, quote(T))
#[1] "quote(x)" "~y" "quote(T)"
此版本评估其参数,这可能是所需内容的第二部分,即值:
some_function2 <- function(...){
print(sapply(list(...), deparse))
}
z <- quote(T)
some_function2(quote(x), ~y, z)
#[1] "x" "~y" "T"
some_function2(quote(x), ~y, quote(T))
#[1] "x" "~y" "T"