match.arg()

Douglas Bates bates@stat.wisc.edu
Tue, 6 Jan 1998 13:42:59 -0600 (CST)


$ R -norestore

R : Copyright 1997, Robert Gentleman and Ross Ihaka
Version 0.61.0 Alpha (December 21, 1997)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type	"license()" for details.

Type	"demo()" for some demos, "help()" for on-line help, or
	"help.start()" for a HTML browser interface to help.

> foo <- function(a = c("bar", "baz", "boz")) match.arg(a)
> foo("baz")
[1] "baz"
> foo()  # should return "bar" but doesn't
[1] "bar" "baz" "boz"

The current definition of match.arg is
 "match.arg" <-
   function (arg, choices) 
 {
   if (missing(choices)) {
     formal.args <- formals(sys.function(sys.parent()))
     choices <- eval(formal.args[[deparse(substitute(arg))]])
   }
   i <- pmatch(arg, choices)
   if (is.na(i)) 
     stop(paste("ARG should be one of", paste(choices, 
					      collapse = ", "), sep = " "))
   else if (i == 0) 
     if (arg == choices) 
       rval <- choices[1]
     else stop("there is more than one match in match.arg")
   else rval <- choices[i]
   return(rval)
 }

I believe what is happening is that arg is being evaluated when passed 
to match.arg hence it takes on the default value which is the same
value as choices will assume.  Another R-like language defines this
function with the first statement like

   if (missing(choices)) {
     formal.args <- formals(sys.function(sys.parent()))
     choices <- eval(formal.args[[deparse(substitute(arg))]])
     arg <- arg[1]
   }

I think that creates the desired behaviour here too.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._