match_arg: Improved match.arg with better errors and case folding.

match_argR Documentation

Improved match.arg with better errors and case folding.

Description

This function works identically to match.arg(), except that the error messages include the actual name of the argument. So for example, if the argument name is "size", instead of a generic error message "'arg' must be of length 1", you will get "'size' must be of length 1". This makes it easier for users to figure out which argument has been passed incorrectly.

Usage

match_arg(
  arg,
  choices,
  several.ok = FALSE,
  arg_name = substitute(arg),
  ignore.case = FALSE
)

Arguments

arg

a character vector (of length one unless several.ok is TRUE) or NULL which means to take choices[1].

choices

a character vector of candidate values, often missing, see ‘Details’.

several.ok

logical specifying if arg should be allowed to have more than one element.

arg_name

The name of the argument. Normally you do not need to specify this yourself, as it is detected automatically. However, if your variable name is not the name of the argument, you will need to specify this.

ignore.case

If TRUE, case will be ignored when comparing the argument to the choices. Note that the matching choice will be returned in its original case, regardless of the case of arg.

Examples


choices <- c("red", "green", "blue")
color <- "blue"
match_arg(color, choices)
color <- "pink"
try(match_arg(color, choices))
try(match_arg(color, choices, arg_name = "colour"))
color <- "BLUE"
match_arg(color, choices, ignore.case = TRUE)
try(match_arg(color, choices))


DarwinAwardWinner/rctutils documentation built on July 22, 2022, 5:19 a.m.