函数OUTSIDE函数有多少个参数

时间:2022-03-17 15:59:15

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

在R中,如何确定函数所期望的参数数量?

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

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

所以我想定义一个函数f,其中:

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.

基本上,我想从nargs()的实用程序,但从有问题的函数外部。

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.

上面的原因是,我需要知道函数所期望的参数个数,对于optim(...)的特定实现,其中优化的函数是在运行时确定并生成的。

1 个解决方案

#1


8  

A possible approach:

一种可能的方法:

b <- function(x, y) {}
length(formals(b))
# [1] 2

#1


8  

A possible approach:

一种可能的方法:

b <- function(x, y) {}
length(formals(b))
# [1] 2