Reputation: 32416
What is a nice way to pass a default argument to all the unspecified arguments of a function? For example, using list.dirs
there are three arguments: path
, full.names
, and recursive
. In this example. I would like to supply the path argument and then a default argument (FALSE) that gets passed to all the remaining function arguments. In this example it is obviously no problem to simply specify FALSE
to the two remaining arguments, but what if there were a lot of arguments that I wanted to specify with the default?
Something equivalent to
list.dirs(path=".", full.names = FALSE, recursive = FALSE)
but with something like (which doesn't work)
do.call("list.dirs", list(path=".", FALSE))
or (also doesn't work)
do.call("list.dirs", list(path=".", rep(FALSE, 2)))
Upvotes: 1
Views: 90
Reputation: 1294
Use formals()
to return a list of arguments, length() to get the number of arguments, and replicate to create a list of 'default' arguments the right length (minus the arguments set manually).
do.call("list.dirs", c(list("."), replicate(length(formals("list.dirs"))-1,expr = "FALSE")))
Upvotes: 2