match.arg
Verify an Argument Using Partial Matching

Description

Finds a full argument name when given a partial match.

Usage

match.arg(arg, choices, several.ok = FALSE)

Arguments

arg A character argument. Can be abbreviated.
choices A character vector of candidate values for arg.
several.ok A logical value. If TRUE, arg can contain multiple elements, each of which is matched. If FALSE (the default), and if arg contains more than one element, the call results in an error.

Details

The usage match.arg(what) works when the function calling match.arg has an argument named what, for which a character vector default is included in the argument list. In this case, match.arg uses this default vector for choices, and if the default was used in the call, returns the first value.
Value
returns the unabbreviated version of arg, if a unique partial match with one of the elements of choices is found. If no unique match is found, the call results in an error.
See Also
charmatch, char.expand, function, match, pmatch.
Examples
# There are two ways to use the function.
# One way is to give the choices when you call match.arg:
myfun <- function(type = "response", ...){
  match.arg(type, choices = c("response","link","terms"))
}

# The other way is to give the choices in the argument list to the function: myfun <- function(type = c("response","link","terms"), ...){ match.arg(type) }

# Results are the same with either of the above. myfun() # [1] "response" myfun("li") # [1] "link" myfun("le") # Gives the following error message: # Error: 'arg' should be one of: "response", "link", "terms" myfun("linki") # same error message as previous example

# You can also use the first way with no default value myfun <- function(type, ...){ match.arg(type, choices = c("response","link","terms")) } # With no default, this fails: myfun()

# Specify several.ok=TRUE to get the unique # partial match for multiple strings match.arg(c("li","r","t"), c("response","link","terms"), several.ok=TRUE) # [1] "link" "response" "terms"

Package base version 6.0.0-69
Package Index