获取R中的函数名称

use*_*085 1 variables r function

我有一个函数 f:

f=function(){}
Run Code Online (Sandbox Code Playgroud)

和一个变量

var=f
Run Code Online (Sandbox Code Playgroud)

我想知道如何使用 var 获取函数 f 的名称?我试过 :

as.character(quote(var))
Run Code Online (Sandbox Code Playgroud)

但我得到“var”,如果我尝试 eval(var) ,我得到 function(){}

有没有人有办法解决吗 ?谢谢

Mit*_*tra 5

当您这样做时:var=f您实际上是在创建一个varf. 总之,您无法访问f您给出的示例中的名称,因为 R 没有保留任何历史记录。

我不确定您到底想做什么,但以下内容可能会有所帮助:

#definition of the function f
f=function(){}
#assignation of the reference 'f' instead of the content of f with substitute()
var=substitute(f)
#checking what is inside var
var
> f

#if you need to use f through var, you have to use eval()
eval(var)
> function(){}
Run Code Online (Sandbox Code Playgroud)