可以用来sys.call()
访问调用者给出的函数参数。必须小心,因为sys.call()
不会评估参数,而是为您提供调用的表达式。当函数...
作为参数调用时,这变得特别困难:sys.call()
将只包含...
,而不是它们的值。但是,可以将sys.call()
和评估它作为另一个函数的参数列表,例如list()
。这会评估所有承诺并丢弃一些信息,但是当我试图规避 R 的内部匹配时,我看不出如何解决这个问题。
一种想法是模拟严格匹配。我附加了一个辅助函数,如果将其作为函数中的第一个命令调用,它就可以做到这一点:
fun = function(x, base='', ...) {
strictify() # make matching strict
list(x, base, ...)
}
这会过滤掉不匹配的参数:
> fun(10, b = 20)
[[1]]
[1] 10
[[2]]
[1] ""
$b
[1] 20
并且也应该在大多数其他情况下工作(有或没有...
, 右边...
有参数 , 有参数默认值)。它唯一不适用的是非标准评估,例如,当尝试使用substitute(arg)
.
辅助函数
strictify <- function() {
# remove argument values from the function
# since matching already happened
parenv <- parent.frame() # environment of the calling function
rm(list=ls(parenv), envir=parenv) # clear that environment
# get the arguments
scall <- sys.call(-1) # 'call' of the calling function
callingfun <- scall[[1]]
scall[[1]] <- quote(`list`)
args <- eval.parent(scall, 2) # 'args' is now a list with all arguments
# if none of the argument are named, we need to set the
# names() of args explicitly
if (is.null(names(args))) {
names(args) <- rep("", length(args))
}
# get the function header ('formals') of the calling function
callfun.object <- eval.parent(callingfun, 2)
callfun.header <- formals(callfun.object)
# create a dummy function that just gives us a link to its environment.
# We will use this environment to access the parameter values. We
# are not using the parameter values directly, since the default
# parameter evaluation of R is pretty complicated.
# (Consider fun <- function(x=y, y=x) { x } -- fun(x=3) and
# fun(y=3) both return 3)
dummyfun <- call("function", callfun.header, quote(environment()))
dummyfun <- eval(dummyfun, envir=environment(callfun.object))
parnames <- names(callfun.header)
# Sort out the parameters that didn't match anything
argsplit <- split(args, names(args) %in% c("", parnames))
matching.args <- c(list(), argsplit$`TRUE`)
nonmatching.arg.names <- names(argsplit$`FALSE`)
# collect all arguments that match something (or are just
# positional) into 'parenv'. If this includes '...', it will
# be overwritten later.
source.env <- do.call(dummyfun, matching.args)
for (varname in ls(source.env, all.names=TRUE)) {
parenv[[varname]] <- source.env[[varname]]
}
if (!"..." %in% parnames) {
# Check if some parameters did not match. It is possible to get
# here if an argument only partially matches.
if (length(nonmatching.arg.names)) {
stop(sprintf("Nonmatching arguments: %s",
paste(nonmatching.arg.names, collapse=", ")))
}
} else {
# we manually collect all arguments that fall into '...'. This is
# not trivial. First we look how many arguments before the '...'
# were not matched by a named argument:
open.args <- setdiff(parnames, names(args))
taken.unnamed.args <- min(which(open.args == "...")) - 1
# We throw all parameters that are unmatched into the '...', but we
# remove the first `taken.unnamed.args` from this, since they go on
# filling the unmatched parameters before the '...'.
unmatched <- args[!names(args) %in% parnames]
unmatched[which(names(unmatched) == "")[seq_len(taken.unnamed.args)]] <- NULL
# we can just copy the '...' from a dummy environment that we create
# here.
dotsenv <- do.call(function(...) environment(), unmatched)
parenv[["..."]] <- dotsenv[["..."]]
}
}
也可以有一个将正常匹配函数转换为严格匹配函数的函数,例如
strict.fun = strictificate(fun)
但这将使用相同的技巧。