I'm in the process of developing a framework in R and would like one of my functions to be multi-purposed. I would like to achieve this by requiring that exactly one set of arguments be passed. In other words, I would like to write a function foo
which requires either the arguments x
and y
OR the argument a
. If neither set is provided, if a set is incomplete, or if both sets are provided, an error should be thrown.
One way to achieve this is using only optional arguments, followed by if
statements. This is demonstrated below. However, I would like to do this more elegantly.
foo <- function(x, y, a){
if (!missing(a) & missing(x) & missing(y)){
a
return("Only a provided")
} else if (missing(a) & !missing(x) & !missing(y)){
x; y
return("x and y provided")
} else {
stop("No complete and/or distinct argument set provided")
}
The function should work as follows:
> foo(a = 1)
[1] "Only a provided"
> foo(x = 1, y = 2)
[1] "x and y provided"
> foo()
Error in foo() : No complete and/or distinct argument set provided
> foo(x = 1)
Error in foo(x = 1) : No complete and/or distinct argument set provided
> foo(x = 1, y = 2, a = 3)
Error in foo(x = 1, y = 2, a = 3) :
No complete and/or distinct argument set provided
Extra credit for also including a generalized answer that can handle any number of argument sets of any size.
Aside: the above example uses missing()
and no argument defaults, but this is by no means a requirement. I am flexible on using various formats so long as they offer a good solution to the question at hand.