[R] How to count the number of parameters in a function

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Apr 9 10:09:07 CEST 2003


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

> 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?

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 ... .


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list