From Martin Maechler <maechler@stat.math.ethz.ch>  Mon Feb 24 15:46:02 2003
From: Martin Maechler <maechler@stat.math.ethz.ch> (Martin Maechler)
Date: Mon Feb 24 15:46:02 2003
Subject: [Rd] match.arg
In-Reply-To: <3E518FBB.2080405@pdf.com>
References: <3E518FBB.2080405@pdf.com>
Message-ID: <15962.12269.732529.587667@gargle.gargle.HOWL>

>>>>> "Spencer" == Spencer Graves <spencer.graves@pdf.com>
>>>>>     on Mon, 17 Feb 2003 17:43:23 -0800 writes:

    Spencer> Hello: I'm not on the "r-devel" list, but I just
    Spencer> modified "match.arg" from R 1.6.2 for Windows to
    Spencer> accept a vector for "arg".

    Spencer> 	  Is it appropriate that I send such things to
    Spencer> this email address for consideration for inclusion
    Spencer> in a future release?

yes (using "R-devel" for ...). 
But why is this current proposal really useful?
Note that I can't see reason to match more than one argument to the
*same* list of choices. `choices' already is a vector typically...

Making a function more complicated makes it also more
error-prone, so I think we'd need a good motivation for it.
E.g., your proposal below quite badly fails when
length(arg) == 0, since you've used the ``well-known to be unsafe''
1:length(arg) idiom instead of the safe  seq(length= length(arg)) one.

Regards,
Martin

    Spencer> 	  I just compared this with "match.arg" in
    Spencer> S-Plus 6.1 for Windows.  There, I got the
    Spencer> following:

    >> match.arg(c("a","b"), c("aa", "bb"))
    Spencer> [1] "aa" "bb"




    Spencer> 	  However, match.arg(c("a", "b")) in a test
    Spencer> function with "default" = c("aa", "bb") returned
    Spencer> only "a"; the following returns c("aa", "bb").

    Spencer> Thanks for all your hard work in developing this
    Spencer> marvelous product.

    Spencer> Sincerely, Spencer Graves

    match.arg <-
    function (arg, choices)
    {
	 if (missing(choices)) {
	     formal.args <- formals(sys.function(sys.parent()))
	     choices <- eval(formal.args[[deparse(substitute(arg))]])
	 }
    #	 cat("choices =", choices, "\n")
	     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 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
    }



[Rd] match.arg
Martin Maechler