[Rd] Retrieving function name

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Nov 9 16:52:26 CET 2006


"Tom McCallum" <tom.mccallum at levelelimited.com> writes:

> Hi,
> 
> Does anyone know how I can retrieve a function name, for example
> 
> If I have a function f as follows:
> 
> f <- function( myfunc ) {
> 	print( name_of(myfunc) );
> }
> 
> I want to know what I should have as "name_of" such that I could call this  
> with :
> 	f( median )
> and it would print "median"
> 
> or f( my_function_name ) and it would print "m_function_name".
> 
> So far all I can get is the function definition that myfunc points to.
> 
> I thought the structure command might do it but this also just gives back  
> the function definition and not the original name.
> 
> Any suggestions?

Depending on what you really want, this is either impossible or
trivial. The trivial version is 

f <- function(x) print(deparse(substitute(x)))

and the impossible one is to get something that prints "mean" if you
do something like

x<-1
f(switch(x, 1 = mean, 2 = median, 3 = sd, 4 = IQR))

or 

g <- function(foo) f(foo)
g(mean)

or indeed does anything sensible with

f(function(x,y) x*y)


Thing is, functions do not "have names", they can be anonymous or
assigned to multiple names, or be passed as arguments.

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-devel mailing list