match.arg(arg, choices, several.ok = FALSE)
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. |
# 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"