Skip to content

Instantly share code, notes, and snippets.

@franzbischoff
Last active July 9, 2020 23:32
Show Gist options
  • Save franzbischoff/9036503ae60f2f21eee6ee1190d011ac to your computer and use it in GitHub Desktop.
Save franzbischoff/9036503ae60f2f21eee6ee1190d011ac to your computer and use it in GitHub Desktop.
How to verify if arguments were called, the difficult way
myfunction <- function (a, b, c, ...)
{
p <- list()
# use formals to get the arguments from a function.
# use sys.function() to get the own function. if you return sys.function() you return itself
formal.args <- formals(sys.function())
formal.args[["..."]] <- NULL # this is not needed, but here you can remove the "..." argument
for (arg in names(formal.args)) {
# this tests if you pass "" to the argument or if you
# doesn't call it at all, and remove it from the argument list
if (as.logical(mget(arg) != "")) {
p[arg] <- list(get(arg))
}
}
dot.args <- list(...) # you thought you had remove it? You had, but from `formal.args`
names.dot.args <- names(dot.args)
# here you add any new argument you got from the `...` and add it as a named argument
if (length(dot.args) > 0) {
for (i in 1:length(dot.args)) {
p[[names.dot.args[i]]] <- dot.args[[i]]
}
}
return(p) # just returning something, in this case the arguments that were filtered...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment