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

Randall Pruim rpruim at calvin.edu
Tue Oct 20 23:06:16 CEST 2015


I think perhaps it would be useful if you tipped your hand a bit more about what you are actually trying to do with your function, but I think that this does the immediate task for functions that have … in the argument list.


foo <- function(x=1, y=2, z=3, ...) {
  m <- length(formals()) + length(list(...)) - 1
  m
}

The -1 is because … gets counted in the formal arguments, but we don’t want that in m.

Of course, for a particular function, you could replace length(formals()) with the number of formals you actually have, since this won’t change.

But perhaps I’m misunderstanding the overall goal.

—rjp

On Oct 20, 2015, at 3:45 PM, Steven Stoline <sstoline at gmail.com<mailto: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

}




with thanks
Steve

--
Steven M. Stoline
1123 Forest Avenue
Portland, ME 04112
sstoline at gmail.com<mailto:sstoline at gmail.com>

[[alternative HTML version deleted]]

_______________________________________________
R-sig-teaching at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-teaching


	[[alternative HTML version deleted]]



More information about the R-sig-teaching mailing list