match.arg(arg, choices)
arg.
arg if there is a unique partial match
with one of the elements of
choices.
If there is no unique match, the call results in an error.
The usage
match.arg(what) will work 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 in the event that the default was used in the call, returns the first value.
# 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 in call to "myfun": Argument "type" should be one of "response",
"link", "terms"
Dumped
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()