I want to call a function but depending on the situation I might call it with extra arguments or not. Here is a simple example:
FUN <- function(arg1 = "default1", arg2 = "default2", arg3 = "default3")
print(list(arg1, arg2, arg3))
x1 <- "hi"
x2 <- TRUE
x3 <- 1:3
use.arg3 <- FALSE # This will decide if `x3` is used or not.
if (use.arg3) {
FUN(arg1 = x1, arg2 = x2, arg3 = x3)
} else {
FUN(arg1 = x1, arg2 = x2)
}
While the code is clear, it feels a little redundant. Also imagine that if I had similar use.arg1
and use.arg2
variables, I would have an ugly mix of possibilities (8)...
I have a solution posted below but I find it a little complicated, to the point that I always struggle to remember the exact syntax.
If you have a better idea, thank you for sharing.