I'm trying something with a package function. But I'm getting an error downstream that is making me wonder if I am doing things right. Or perhaps I am overcomplicating the use of ...
and friends.
my_fun <- function(...) {
args <- rlang::dots_list(
param1 = "a", # some default values
param2 = "b",
...,
.homonyms = "last" # allow user-supplied args to supersede defaults
)
param1 <- f(args$param1)
param2 <- f(args$param2)
args <- rlang::dots_list(
!!!args, # splice in original defaults
param1,
param2,
.homonyms = "last",
.named = TRUE
)
output_fun(!!!args)
}
The function can use a long list of arguments, but mostly I am happy with them having the default values. But I want to be able to pass in a different value occasionally via the dots. The defaults are listed in the args list at the top of the function, but anything passed in by the user should supersede the defaults.
The reason I am doing this is to avoid having lots of named function parameters with default values, to document. The output function at the end then uses the updated list of args.
One of the args passed on my output function is a date-time value that behaves fine when I pass it to output_fun
like this:
output_fun(param1, param2, dttm = args$dttm, another = args$another)
but when I include it within this wrapper function with the dots/splice setup I get an error downstream: Error in !args : invalid argument type
which the traceback tells me is something to do with the datetime value.