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.
A possible approach:
b <- function(x, y) {}
length(formals(b))
# [1] 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With