我正在尝试捕获函数调用的所有参数,因此使用了deparse和replace。在我想使用3个点作为参数扩展函数的用法之前,它一直工作良好。原来只有第一个参数被捕获。该功能的简化版本如下所示。
func_3dot <- function(...){
tmp <- deparse(substitute(...))
print(tmp)
}
对于下面的示例,[1]应为“ mod_1”和“ mod_2”。有什么办法可以在不改变上述功能的情况下仍然捕获所有输入参数?谢谢!!
mod_1 <- lm(mpg ~ wt, data = mtcars)
mod_2 <- update(mod_1, . ~ . + hp)
> func_3dot(mod_1, mod_2)
[1] "mod_1"
答案 0 :(得分:0)
使用list(...)
而不是...
,然后分别解析条目。例如,
func_3dot <- function(...){
tmp <- substitute(list(...))
sapply(tmp, deparse)[-1]
}
[-1]
从已删除的结果中删除对list
的呼叫。
答案 1 :(得分:0)
您可以使用match.call
:
func_3dot <- function(...){
tmp <- vapply(as.list((match.call()[-1])), deparse, FUN.VALUE = character(1))
print(tmp)
}
func_3dot(mod_1, mod_2)
#[1] "mod_1" "mod_2"
答案 2 :(得分:0)
One option with purrr
and rlang
library(rlang)
library(purrr)
func_3dot <- function(...){
unname(map_chr(quos(...), quo_name))
}
func_3dot(mod_1, mod_2)
#[1] "mod_1" "mod_2"
Or using base R
func_3dot <- function(...){
sapply(as.list(substitute(...())), deparse)
}
func_3dot(mod_1, mod_2)
#[1] "mod_1" "mod_2"