Skip to content

How to count the number of parameters in a function

3 messages · arnaud_amsellem@ssga.com, Brian Ripley, Torsten Hothorn

#
I have the following function:
Myfunc <- function(var1,var2,.....,varN)
{ .....
}
In the above function I have a variable number of parameters (N>2). How can
I count how many parameters have been entered?

Any help appreciated

Thanks

Arno
#
On Wed, 9 Apr 2003 arnaud_amsellem at ssga.com wrote:

            
Well, that example will not parse. If you had

Myfunc <- function(...)
{
    dots <- list(...)
    cat("#args is", length(dots), "\n")
}

you would be able to see how it might be done.

Another way is to use match.call(expand.dots=TRUE), as in

Myfunc <- function(...)
{
    Call <- match.call(expand.dots=TRUE)
    cat("#args is", length(Call) - 1, "\n")
}

the first element being the function name.  In this version you can have
named formal arguments and ... .
#
On Wed, 9 Apr 2003 arnaud_amsellem at ssga.com wrote:

            
using `lm' as example:

length(formals(lm))

best,

Torsten