[R] take precisely one named argument

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Fri Dec 17 14:47:34 CET 2004


On 17-Dec-04 Ted Harding wrote:
> I don't know the *best* way (expert R anatomists will know ... )
> but the following dirty handed modification seems to do what
> you want:
> 
>   f <- function(z=NULL, a=NULL, b=NULL){
>      if(!is.null(z)){
>        stop("usage: f(a=...) or f(b=...)")
>      }
>      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)}
>   }
> 
> (This traps attempts to use f() with an un-named argument).
> 
> Ted.

Playing around with the above shows that not only does it
give the correct response for correct usage, e.g. f(a=3) or f(b=3),
but, serendipitously, it gives appropriate responses for just
about any way you could think of using it wrongly:

> f(3)
Error in f(3) : usage: f(a=...) or f(b=...)

> f(a=2,b=3)
Error in f(a = 2, b = 3) : specify exactly one of a and b

> f(d=3)
Error in f(d = 3) : unused argument(s) (d ...)

> f(a=2,d=3)
Error in f(a = 2, d = 3) : unused argument(s) (d ...)

etc.

Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861  [NB: New number!]
Date: 17-Dec-04                                       Time: 13:47:34
------------------------------ XFMail ------------------------------




More information about the R-help mailing list