| MatchArg | R Documentation |
A robust argument matching function that supports exact matching, partial matching, and provides sensible defaults when no match is found. This is an internal utility function not intended for direct use by package users.
MatchArg(arg, choices, default = choices[1L], call = rlang::caller_env(), ...)
arg |
The argument to match against choices. Can be |
choices |
A character vector of valid choices to match against. |
default |
The default value to return if no match is found and |
call |
caller env |
... |
No usage |
This function provides a more flexible alternative to base::match.arg() with
the following matching strategy:
If arg is NULL, returns the default value
Attempts exact matching using base::match()
Falls back to partial matching using base::pmatch()
If no match found and default is not NULL, returns default
Otherwise, throws an informative error with valid choices
The function uses rlang::caller_env() for accurate error reporting in the
context where the function was called.
Returns the matched choice from the choices vector. If no match is found
and arg is NULL, returns the default value. If no match is found and
arg is not NULL, throws an informative error.
match for exact matching \
pmatch for partial matching \
caller_env for calling environment context
## Not run:
# Internal usage examples
MatchArg("app", c("apple", "banana", "application")) # Returns "apple"
MatchArg(NULL, c("red", "green", "blue")) # Returns "red" (default)
MatchArg("gr", c("red", "green", "blue")) # Returns "green"
# Would error: MatchArg("invalid", c("valid1", "valid2"))
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.