The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

Argument Matching

2024-01-31

Ignoring case

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.

base::match.arg("Y", c("yes", "no"))
#> 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.

strex::match_arg("Y", c("yes", "no"))
#> Error in `str_match_arg_basic()`:
#> ! `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`.
strex::match_arg("Y", c("yes", "no"), ignore_case = TRUE)
#> [1] "yes"

Error Messages

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.

No matches

choices <- c("Apples", "Pears", "Bananas", "Oranges", "Avocados", "Apricots")
match.arg("Q", choices)
#> Error in match.arg("Q", choices): 'arg' should be one of "Apples", "Pears", "Bananas", "Oranges", "Avocados", "Apricots"
strex::match_arg("Q", choices)
#> Error in `str_match_arg_basic()`:
#> ! `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`.

Multiple matches

match.arg("A", choices)
#> Error in match.arg("A", choices): 'arg' should be one of "Apples", "Pears", "Bananas", "Oranges", "Avocados", "Apricots"
strex::match_arg("A", choices)
#> Error in `str_match_arg_basic()`:
#> ! `arg` must be a prefix of exactly one element of `choices`.
#> ✖ Your `arg`, 'A', is a prefix of two or more elements of `choices`.
#> ℹ The first two of these are 'Apples' and 'Avocados'.

Wrong arg length

match.arg(c("A", "a"), choices)
#> Error in match.arg(c("A", "a"), choices): 'arg' must be of length 1
strex::match_arg(c("A", "a"), choices)
#> Error in `str_match_arg_basic()`:
#> ! `arg` must have length 1.
#> ✖ Your `arg` has length 2.
#> ℹ To use an `arg` with length greater than one, use `several_ok = TRUE`.

Duplicate elements in choices

choices <- c(choices, "Pears")
match.arg("P", choices)
#> Error in match.arg("P", choices): 'arg' should be one of "Apples", "Pears", "Bananas", "Oranges", "Avocados", "Apricots"
strex::match_arg("P", choices)
#> Error in `str_match_arg_basic()`:
#> ! `choices` must not have duplicate elements.
#> • Element 7 of your `choices`, 'Pears', is a duplicate.

Not specifying choices

It’s OK not to specify choices in one circumstance: when arg is passed as a default argument of another function.

myword <- function(w = c("abacus", "baseball", "candy")) {
  w <- strex::match_arg(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.

myword <- function(w = c("abacus", "baseball", "candy")) {
  w <- strex::match_arg(identity(w))
  w
}
myword("b")
#> Error in `strex::match_arg()`:
#> ! You used `match_arg()` without specifying a `choices` argument.
#> ℹ The only way to do this is from another function where the `arg` has a default setting. This is the same as `base::match.arg()`.
#> ℹ See the man page for `match_arg()`.
#> ℹ See the vignette on argument matching: enter `vignette('argument-matching', package = 'strex')` at the R console.
myword <- function(w = c("abacus", "baseball", "candy")) {
  w <- strex::match_arg(as.character(w))
  w
}
myword("b")
#> Error in `strex::match_arg()`:
#> ! You used `match_arg()` without specifying a `choices` argument.
#> ℹ The only way to do this is from another function where the `arg` has a default setting. This is the same as `base::match.arg()`.
#> ℹ See the man page for `match_arg()`.
#> ℹ See the vignette on argument matching: enter `vignette('argument-matching', package = 'strex')` at the R console.

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.