[R] take precisely one named argument
Gabor Grothendieck
ggrothendieck at myway.com
Fri Dec 17 17:16:26 CET 2004
Gabor Grothendieck <ggrothendieck <at> myway.com> writes:
:
: Robin Hankin <r.hankin <at> soc.soton.ac.uk> writes:
:
: :
: : Hi
: :
: : I want a function that takes precisely one named argument and no
: : unnamed arguments. The named argument must be one of "a" or "b".
: :
: : If "a" is supplied, return "a". If "b" is supplied, return 2*b.
: : That is, the desired behaviour is:
: :
: : R> f(a=4) #return 4
: : R> f(b=33) #return 66
: : R> f(5) #error
: : R> f(a=3,b=5) #error
: : R> f(a=3,q=3) #error
: : R> f(q=3) #error
: :
: : The following function is intended to implement this:
: :
: : f <- function(a=NULL, b=NULL){
: : if(!xor(is.null(a), is.null(b))){stop("specify exactly one of a and
: : b")}
: : if(is.null(a)){return(2*b)}else{return(a)}
: : }
: :
: : It almost works, but f(6) returns 6 (and should be an error).
: :
: : What is the best way to accomplish my desired behaviour?
:
: Here is one way to do it. nm are the names (where the [-1] removes
: the function name). The ... traps any arg that is not a or b and the
: stopifnot conditions ensure that exactly one of a and b are specified.
:
: ff <- function(..., a = 0, b = 0) {
: nm <- names(match.call()[-1])
: stopifnot(length(nm) == 1, nm %in% c("a", "b"))
: a+2*b
: }
:
: One thing to watch out for is that if
Seems this got cut off somehow.
I was going to mention that while the above works, if the variables
are aa and bb rather than a and b then ... results in only exact matches
working. That is a or b will be trapped by ... so only aa written
exactly and bb written exactly will match the aa and bb args.
Also, one could replace c("a", "b") with names(formals()[-1]) which
would make it easier to change variable names or add more variable
names in the future since the condition would not change.
More information about the R-help
mailing list