R:向量化解析(替代(x))
R: Vectorize deparse(substitute(x))
我想矢量化 deparse(substitute(x))
。
f <- function(...){
deparse(substitute(...))
}
f(a, b, c)
这只给出了第一个元素,"a"
,但我等待 "a", "b", "c"
。无意间,我发现了这个
f2 <- function(...){
deparse(substitute(...()))
}
f2(ab, b, c)
这得到 "pairlist(ab, b, c)"
。现在我可以删除所有不需要的东西 "a", "b", "c"
。但这对我来说似乎并不优雅。有没有办法向量化 deparse(substitute(x))?
我知道 有类似的问题,但答案不包括 deparse(substitute(x))
。
我们可以使用match.call
f <- function(...) sapply(as.list(match.call())[-1], as.character)
f(a)
#[1] "a"
f(a, b)
#[1] "a" "b"
f(a, b, c)
#[1] "a" "b" "c"
或使用substitute
f <- function(...) sapply(substitute(...()), deparse)
f(a)
#[1] "a"
f(a, b)
#[1] "a" "b"
f(a, b, c)
#[1] "a" "b" "c"
match.call
是一个很好的起点。我鼓励您探索所有可以用它做的事情。我相信这会让你到达你想要的地方:
f <- function(...){
as.character(match.call(expand.dots = FALSE)[[2]])
}
以及使用它的示例...
f(hey, you)
[1] "hey" "you"
我想矢量化 deparse(substitute(x))
。
f <- function(...){
deparse(substitute(...))
}
f(a, b, c)
这只给出了第一个元素,"a"
,但我等待 "a", "b", "c"
。无意间,我发现了这个
f2 <- function(...){
deparse(substitute(...()))
}
f2(ab, b, c)
这得到 "pairlist(ab, b, c)"
。现在我可以删除所有不需要的东西 "a", "b", "c"
。但这对我来说似乎并不优雅。有没有办法向量化 deparse(substitute(x))?
我知道 deparse(substitute(x))
。
我们可以使用match.call
f <- function(...) sapply(as.list(match.call())[-1], as.character)
f(a)
#[1] "a"
f(a, b)
#[1] "a" "b"
f(a, b, c)
#[1] "a" "b" "c"
或使用substitute
f <- function(...) sapply(substitute(...()), deparse)
f(a)
#[1] "a"
f(a, b)
#[1] "a" "b"
f(a, b, c)
#[1] "a" "b" "c"
match.call
是一个很好的起点。我鼓励您探索所有可以用它做的事情。我相信这会让你到达你想要的地方:
f <- function(...){
as.character(match.call(expand.dots = FALSE)[[2]])
}
以及使用它的示例...
f(hey, you)
[1] "hey" "you"