获取R中的函数名称 [英] Get name of function in R

查看:53
本文介绍了获取R中的函数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数f:

f=function(){}

和一个变量

var=f

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

I would like to know how to get the name of the function f using var ? I tried :

as.character(quote(var))

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

but I get "var" and if I try eval(var) , I get function(){}

有人可以解决吗?谢谢

推荐答案

当您执行以下操作: var = f 时,您实际上是在创建具有相同内容的新变量 var 作为 f .总之,您不能在给出的示例中访问 f 的名称,因为R没有保留任何历史记录.

When you do: var=f you are actually creating a new variable var with the same content as f. In summary, you can not access the name of f in the example you gave since R is not keeping any history of this.

我不确定您要确切执行的操作,但是以下操作可能会有所帮助:

I am not sure what you want to do exactly, but the following might be helpful:

#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(){}

这篇关于获取R中的函数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆