[R] Retrieve ... argument values
Richard A. O'Keefe
ok at cs.otago.ac.nz
Wed Sep 17 07:58:00 CEST 2003
Ben Bolker <bolker at zoo.ufl.edu> wrote:
Yes, although this becomes tedious if (e.g.) you have a function that
calls two different functions, each of which has many arguments (e.g.
plot() and barplot(); then you have to set up a whole lot of arguments
that default to NULL and, more annoyingly, you have to document them all
in any .Rd file you create -- rather than just having a ... argument which
you can say should contain arguments for either of the subfunctions (as
long as the arguments don't overlap, of course)
Could I suggest a very simple alternative, which extends gracefully to
any number of arguments, and doesn't require you to know anything much
about ... except that you can pass it to another function?
> f <- function(...) {
+ has.ylim <- function(..., ylim) return(!missing(ylim))
+
+ if (has.ylim(...)) "yes" else "no"
+ }
> f(xlim=1)
[1] "no"
> f(ylim=2)
[1] "yes"
For each parameter XXX that you want to test the presence of, write
has.XXX <- function(..., XXX) return(!missing(XXX))
and then use
has.XXX(...)
in the body of the main function.
More information about the R-help
mailing list