Last active
December 2, 2022 16:35
-
-
Save jmbarbone/af2470687227e70498b89f2bf4e5749f to your computer and use it in GitHub Desktop.
`do.call()` but with duplicated
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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