我想获取用于my_fun
排除点和第一个参数的参数列表。我还想拥有可选参数的默认值。这就是我目前正在这样做的方式,但我确信有一种更简单的方法:
my_fun <- function(a, b = "b", c = "c", d = c("d1","d2"), ...){
my_call <- as.list(match.call(expand.dots = FALSE))[-1]
my_call[names(my_call) %in% c("a","...")] <- NULL
my_formals <- as.list(formals("my_fun"))
my_formals[names(my_formals) %in% c("a","...")] <- NULL
for (arg in names(my_call)){
my_formals[[arg]] <- my_call[[arg]]
}
my_formals$d <- eval(my_formals$d)
my_formals
}
res <- my_fun("a", c = 3, e = 3, f = 5)
res
# list(b = "b", c = 3, d = c("d1","d2"))
请注意,因为我正在使用formals
我必须做一个尴尬的eval
. 有任何想法吗?