How can we know how many arguments a function take?
For instance, for a given function f
, I\'d like to do:
if (arg_number(f) == 0)
f()
nargs()
: will check the number of arguments from within the function
The Number of Arguments to a Function
Edit:
formals
will give access to the arguments of the function
> f <- function(x, y, z) x + y + z
> formals(f)
> $x
> $y
> $z
Update: (from @Spacedman)
To know the number of arguments,
> length(formals(f))
> [1] 3
Also,
> length(formalArgs(f))
> [1] 3