check_exclusive | R Documentation |
check_exclusive()
checks that only one argument is supplied out of
a set of mutually exclusive arguments. An informative error is
thrown if multiple arguments are supplied.
check_exclusive(..., .require = TRUE, .frame = caller_env(), .call = .frame)
... |
Function arguments. |
.require |
Whether at least one argument must be supplied. |
.frame |
Environment where the arguments in |
.call |
The execution environment of a currently
running function, e.g. |
The supplied argument name as a string. If .require
is
FALSE
and no argument is supplied, the empty string ""
is
returned.
f <- function(x, y) {
switch(
check_exclusive(x, y),
x = message("`x` was supplied."),
y = message("`y` was supplied.")
)
}
# Supplying zero or multiple arguments is forbidden
try(f())
try(f(NULL, NULL))
# The user must supply one of the mutually exclusive arguments
f(NULL)
f(y = NULL)
# With `.require` you can allow zero arguments
f <- function(x, y) {
switch(
check_exclusive(x, y, .require = FALSE),
x = message("`x` was supplied."),
y = message("`y` was supplied."),
message("No arguments were supplied")
)
}
f()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.