Reputation: 720
I have a function f:
f=function(){}
and a variable
var=f
I would like to know how to get the name of the function f using var ? I tried :
as.character(quote(var))
but I get "var" and if I try eval(var) , I get function(){}
Does anyone have a solution ? Thank
Upvotes: 2
Views: 2534
Reputation: 855
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(){}
Upvotes: 8
Reputation: 16080
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(){}
Upvotes: -1