Get name of function in R

前端 未结 2 1404
隐瞒了意图╮
隐瞒了意图╮ 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(){}
    
    0 讨论(0)
  • 2021-01-24 08:31

    var=f assignes function(){} to var. It only knows that it is a function and 'forgets' about variable name. This code:

    f <- function(){}
    var <- f
    

    has the same effect as this:

    var <- function(){}
    
    0 讨论(0)
提交回复
热议问题