[Rd] match.arg
Spencer Graves
spencer.graves@pdf.com
Tue Feb 18 04:41:02 2003
Hello:
I found a way to break the "match.arg" function I sent a few hours ago.
The following works better, I believe.
Thanks again for your great work on the R project.
Spencer Graves
##########################
match.arg <-
function (arg = NULL, choices= NULL)
{
if (is.null(choices)) {
formal.args <- formals(sys.function(sys.parent()))
choices <- eval(formal.args[[deparse(substitute(arg))]])
}
# cat("choices =", choices, "; arg =", arg, "\n")
if((length(arg)==length(choices)) && all(arg==choices))
return(choices[1])
for(j in 1:length(arg)){
if (all(arg[j] == choices))
arg[j] <- choices[1]
else{
i <- pmatch(arg[j], choices)
if (is.na(i))
stop(paste("'arg' =", arg[j],
"should be one of", paste(choices, collapse = ", "),
sep = " "))
if (length(i) > 1)
stop("there is more than one match in match.arg")
arg[j] <- choices[i]
}
}
arg
}