base
The base function match.arg()
is good, but it doesn’t
offer the possiblity to ignore case during argument matching. Sometimes
it’s good to ignore case; for example, if you’re matching the arguments
c("yes", "no")
, there’s no need to worry about case.
::match.arg("Y", c("yes", "no"))
base#> Error in base::match.arg("Y", c("yes", "no")): 'arg' should be one of "yes", "no"
strex
The default behaviour of strex::match_arg()
is to
observe case, but case ignorance can be turned on with
ignore_case = TRUE
.
::match_arg("Y", c("yes", "no"))
strex#> Error in `custom_stop()`:
#> ! `Y` must be a prefix of exactly one element of `choices`.
#> * Your `choices` are "yes", "no".
#> * Your `Y` "Y" is not a prefix of any of your `choices`.
::match_arg("Y", c("yes", "no"), ignore_case = TRUE)
strex#> [1] "yes"
You can begin to see above that the error message from
strex::match_arg()
are more informative and nicely
formatted. Here are a few more examples.
<- c("Apples", "Pears", "Bananas", "Oranges", "Avocados", "Apricots")
choices match.arg("Q", choices)
#> Error in match.arg("Q", choices): 'arg' should be one of "Apples", "Pears", "Bananas", "Oranges", "Avocados", "Apricots"
::match_arg("Q", choices)
strex#> Error in `custom_stop()`:
#> ! `Q` must be a prefix of exactly one element of `choices`.
#> * Your `choices` are "Apples", "Pears", "Bananas", "Oranges", "Avocados", "Apricots".
#> * Your `Q` "Q" is not a prefix of any of your `choices`.
match.arg("A", choices)
#> Error in match.arg("A", choices): 'arg' should be one of "Apples", "Pears", "Bananas", "Oranges", "Avocados", "Apricots"
::match_arg("A", choices)
strex#> Error in `custom_stop()`:
#> ! `A` must be a prefix of exactly one element of `choices`.
#> * Your `A` "A" is a prefix of two or more elements of `choices`.
#> * The first two of these are "Apples" and "Avocados".
arg
lengthmatch.arg(c("A", "a"), choices)
#> Error in match.arg(c("A", "a"), choices): 'arg' must be of length 1
::match_arg(c("A", "a"), choices)
strex#> Error in `custom_stop()`:
#> ! `arg` must have length 1.
#> * Your `arg` has length 2.
#> * To use an `arg` with length greater than one, use `several_ok = TRUE`.
choices
<- c(choices, "Pears")
choices match.arg("P", choices)
#> Error in match.arg("P", choices): 'arg' should be one of "Apples", "Pears", "Bananas", "Oranges", "Avocados", "Apricots"
::match_arg("P", choices)
strex#> Error in `custom_stop()`:
#> ! `choices` must not have duplicate elements.
#> * Element 7, of your `choices` ("Pears") is a duplicate.
choices
It’s OK not to specify choices in one circumstance: when
arg
is passed as a default argument of another
function.
<- function(w = c("abacus", "baseball", "candy")) {
myword <- strex::match_arg(w)
w
w
}myword()
#> [1] "abacus"
myword("b")
#> [1] "baseball"
myword("c")
#> [1] "candy"
This is very strict though, only the symbol for the default argument can be passed, not any variant of it, not even something which evaluates to the same thing.
<- function(w = c("abacus", "baseball", "candy")) {
myword <- strex::match_arg(identity(w))
w
w
}myword("b")
#> Error in `custom_stop()`:
#> ! You have used `strex::match_arg()` without specifying a `choices` argument.
#> * The only way to do this is from another function where `arg` has a default setting. This is the same as `base::match.arg()`.
#> * See the man page for `strex::match_arg()`, particularly the examples: enter `help("strex::match_arg", package = "strex")` at the R console.
#> * See also the vignette on argument matching: enter `vignette("argument-matching", package = "strex")` at the R console.
<- function(w = c("abacus", "baseball", "candy")) {
myword <- strex::match_arg(as.character(w))
w
w
}myword("b")
#> Error in `custom_stop()`:
#> ! You have used `strex::match_arg()` without specifying a `choices` argument.
#> * The only way to do this is from another function where `arg` has a default setting. This is the same as `base::match.arg()`.
#> * See the man page for `strex::match_arg()`, particularly the examples: enter `help("strex::match_arg", package = "strex")` at the R console.
#> * See also the vignette on argument matching: enter `vignette("argument-matching", package = "strex")` at the R console.