Skip to content

Instantly share code, notes, and snippets.

@jmbarbone
Last active December 2, 2022 16:35
Show Gist options
  • Save jmbarbone/af2470687227e70498b89f2bf4e5749f to your computer and use it in GitHub Desktop.
Save jmbarbone/af2470687227e70498b89f2bf4e5749f to your computer and use it in GitHub Desktop.
`do.call()` but with duplicated
do_call <- function(fun, ...) {
fun <- match.fun(fun)
forms <- as.list(formals(fun))
params <- list(...)
nms <- names(params)
o <- order(match(nms, names(forms)))
params <- params[o]
nms <- nms[o]
ok <- nzchar(nms)
none <- which(!ok)
nms <- nms[ok]
# for params without names, use the order in which they are passed
for (no in none) {
forms[[no]] <- params[[no]]
}
# for params with a name, explicitly set those
for (nm in nms) {
forms[[nm]] <- params[[nm]]
}
do.call(fun, forms)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment