[R-sig-teaching] count the number of argument of a function

Michael Weylandt michael.weylandt at gmail.com
Tue Oct 20 21:51:56 CEST 2015


On Tue, Oct 20, 2015 at 2:45 PM, Steven Stoline <sstoline at gmail.com> wrote:

> Dear All:
>
> I am wondering whether there is a way to read and assign the number of
> arguments of a function inside this function.
>
>
> ### For example
>
> fun<-function(x1,x2,x3,...){
>
> m<- number of arguments
>
> }
>
> ### e.g.
>
> fun<-function(x1,x2,x3){
>
> m<- number of arguments
>
> ### m =3 in this case
>
> }
>
> ### e.g.
>
> fun<-function(x1,x2,x3,y1,z1,z2){
>
> m<- number of arguments
>
> ### m =6 in this case
>
> }
>


It's not entirely kosher, but

length(formals(sys.function()))

will work.

f <- function(a, b,c, d, e, f){
    length(formals(sys.function()))
}

f() ## 6

	[[alternative HTML version deleted]]



More information about the R-sig-teaching mailing list