OUTSIDE函数有多少个函数参数

Nic*_*ton 10 arguments r

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

fa = function(x){}
fb = function(x,y){}
fc = function(x,y,z){}
Run Code Online (Sandbox Code Playgroud)

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

f(fa) = 1
f(fb) = 2
f(fc) = 3
Run Code Online (Sandbox Code Playgroud)

等等......

基本上,我想从实用nargs()的功能,但从外面的功能.

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

nei*_*fws 8

一种可能的方法:

b <- function(x, y) {}
length(formals(b))
# [1] 2
Run Code Online (Sandbox Code Playgroud)