Re: [Rd] Retrieving function name

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Thu 09 Nov 2006 - 15:52:26 GMT

"Tom McCallum" <tom.mccallum@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@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Fri Nov 10 02:55:01 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 09 Nov 2006 - 19:31:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.