Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many arguments to a function from OUTSIDE the function

Tags:

arguments

r

In R, how can I determine the number of arguments a function expects?

fa = function(x){}
fb = function(x,y){}
fc = function(x,y,z){}

So I want to define a function, f, where:

f(fa) = 1
f(fb) = 2
f(fc) = 3

and so forth...

Basically, I would like the utility of nargs() but from outside the function in question.

The reason for the above, is that I need to know the number of arguments that a function expects, for a specific implementation of optim(...), where the function being optimised is determined and generated at runtime.

like image 779
Nicholas Hamilton Avatar asked May 02 '17 13:05

Nicholas Hamilton


1 Answers

A possible approach:

b <- function(x, y) {}
length(formals(b))
# [1] 2
like image 111
neilfws Avatar answered Oct 23 '22 18:10

neilfws