[R-sig-teaching] count the number of argument of a function
Randall Pruim
rpruim at calvin.edu
Wed Oct 21 13:30:27 CEST 2015
Maybe. Maybe not. It depends on the (missing) specification of exactly what is wanted. nargs() will count the arguments supplied by the user directly, but will not count arguments that are provided via defaults.
This may or may not be what one wants. Example:
foo <- function(x = 5, ...) {
c( nargs = nargs(), custom = 1 + length(list(...)))
}
foo()
## nargs custom
## 0 1
foo(a = 5)
## nargs custom
## 1 2
foo(x = 5)
## nargs custom
## 1 1
See the help for nargs() for additional information about what nargs() does/doesn’t count.
And yes, R-help is probably a better place for this sort of technical question — especially when no connection to teaching has been made in the query.
—rjp
Sorry to be a bit sarcastic, but if you had not *mis*used
R-SIG-teaching and rather used R-help for asking a somewhat
basic question about R, you would have gotten better answers
more quickly than all the preceding answers:
The answer is to use nargs()
[which even in its name suggests to give what you ask in the
e-mail subject of this thread]
Martin Maechler
[[alternative HTML version deleted]]
More information about the R-sig-teaching
mailing list