I am using rpart() inside a function myFunction(). rpart() accepts several parameters which are handled using the missing() function:
rpart(formula, data, weights, subset, na.action = na.rpart, method, model = FALSE, x = FALSE, y = TRUE, parms, control, cost, ...)
For example, the parameter method can be left unspecified, and is handled inside rpart() using the following code:
if (missing(method)) method <- "whatever default"
How can I pass the argument method as a parameter for myFunction() in the most simple and efficient way so that it handles the default missing argument?
If I do something like
myFunction(foo = 0, method){# somecode; rpart(y ~ x, data = data, method = method)}
then this throws an error,
argument "method" is missing, with no default
I have also tried with functions like rlang::missing() with no success whatsoever.
Of course an option is doing something like passing myFunction(method = NULL) and then using if-else statements to either pass or not pass this argument, but then I have to code each possibility (for 4 arguments that would be 16 calls) and is very clumsy.
Note that I would also like to avoid using the ellipsis, as I want to specifically name my arguments.
MINIMAL REPRODUCIBLE EXAMPLE:
y <- c(0,0.1,0.1,-0.1, 100, 101, 99)
x <- c(1,2,3,4, 100,101,102)
myFunction <- function(x, y,
method,
weights,
subset,
parms){
rpart(formula = y ~ .,
data = data.frame(y, x),
weights = weights,
subset = subset,
parms = parms)
}
myFunction(x,y)
Error in eval(extras, data, env) : argument "weights" is missing, with no default