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?
Tom
--
Dr. Thomas McCallum
Systems Architect,
Level E Limited
ETTC, The King's Buildings
Mayfield Road,
Edinburgh EH9 3JL, UK
Work +44 (0) 131 472 4813
Fax: +44 (0) 131 472 4719
http://www.levelelimited.com
Email: tom at levelelimited.com
Level E is a limited company incorporated in Scotland. The contents of
this e-mail are privileged and/or confidential. If you are not the
intended recipient, please
notify the sender and ensure this e-mail is deleted and not read, copied
or disclosed. It is your responsibility to scan this e-mail and any
attachments for
computer viruses or other defects. Level E does not accept liability for
any loss or damage which may result from this e-mail or any attachment.
E-mail is not secure
and can be intercepted, corrupted or amended. Level E does not accept
liability for errors or omissions arising as a result of interrupted or
defective transmission.
Any views, opinions, conclusions or other information in this e-mail which
do not relate to the business of Level E Limited are not authorised by
Level E. Unless
specifically stated and authorised by Level E, nothing in this e-mail
shall be taken to be an offer or acceptance of any contract of any nature.
E-mail entering or leaving Level E's system is subject to random
monitoring and recording.
probably you're looking for:
f <- function(FUN, x){
list(funName = deparse(substitute(FUN)), value = FUN(x))
}
f(mean, rnorm(10))
f(median, rnorm(10))
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Tom McCallum" <tom.mccallum at levelelimited.com>
To: <r-devel at r-project.org>
Sent: Thursday, November 09, 2006 4:28 PM
Subject: [Rd] Retrieving function name
> 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?
>
> Tom
>
> --
> Dr. Thomas McCallum
> Systems Architect,
> Level E Limited
> ETTC, The King's Buildings
> Mayfield Road,
> Edinburgh EH9 3JL, UK
> Work +44 (0) 131 472 4813
> Fax: +44 (0) 131 472 4719
> http://www.levelelimited.com
> Email: tom at levelelimited.com
>
> Level E is a limited company incorporated in Scotland. The contents
> of
> this e-mail are privileged and/or confidential. If you are not the
> intended recipient, please
> notify the sender and ensure this e-mail is deleted and not read,
> copied
> or disclosed. It is your responsibility to scan this e-mail and any
> attachments for
> computer viruses or other defects. Level E does not accept liability
> for
> any loss or damage which may result from this e-mail or any
> attachment.
> E-mail is not secure
> and can be intercepted, corrupted or amended. Level E does not
> accept
> liability for errors or omissions arising as a result of interrupted
> or
> defective transmission.
> Any views, opinions, conclusions or other information in this e-mail
> which
> do not relate to the business of Level E Limited are not authorised
> by
> Level E. Unless
> specifically stated and authorised by Level E, nothing in this
> e-mail
> shall be taken to be an offer or acceptance of any contract of any
> nature.
> E-mail entering or leaving Level E's system is subject to random
> monitoring and recording.
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
"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