Get name of function in R

前端 未结 2 1412
隐瞒了意图╮
隐瞒了意图╮ 2021-01-24 07:53

I have a function f:

f=function(){}

and a variable

var=f

I would like to know how to get the name of the fun

2条回答
  •  面向向阳花
    2021-01-24 08:31

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

提交回复
热议问题