Peter Langfelder
2012-Aug-29 22:36 UTC
[R] Extracting the name of a function (inverse of match.fun("myFun"))
Hi all,
is there a way to extract the name of a function, i.e. do the reverse
of match.fun applied to a character string? I would like to print out
the name of a function supplied to another function as an argument.
For example:
myFunc = function(x) { x+1 }
applyFunc = function(fnc, x)
{
fnc = match.fun(fnc)
fnc(x)
}
Is there a way to obtain "myFunc" from the argument fnc in applyFnc
the following call is issued?
applyFnc(myFunc, 1)
Thanks,
Peter
Peter Langfelder
2012-Aug-29 23:00 UTC
[R] Extracting the name of a function (inverse of match.fun("myFun"))
On Wed, Aug 29, 2012 at 3:36 PM, Peter Langfelder <peter.langfelder at gmail.com> wrote:> Hi all, > > is there a way to extract the name of a function, i.e. do the reverse > of match.fun applied to a character string? I would like to print out > the name of a function supplied to another function as an argument. > > For example: > > myFunc = function(x) { x+1 } > > applyFunc = function(fnc, x) > { > fnc = match.fun(fnc) > fnc(x) > } > > Is there a way to obtain "myFunc" from the argument fnc in applyFnc > the following call is issued? > > applyFnc(myFunc, 1)...or am I missing the basic fact that since arguments to functions in R are passed by copy, the name is lost/meaningless? Thanks, Peter
Eloi Mercier
2012-Aug-29 23:08 UTC
[R] Extracting the name of a function (inverse of match.fun("myFun"))
On 12-08-29 04:00 PM, Peter Langfelder wrote:> On Wed, Aug 29, 2012 at 3:36 PM, Peter Langfelder > <peter.langfelder at gmail.com> wrote: >> Hi all, >> >> is there a way to extract the name of a function, i.e. do the reverse >> of match.fun applied to a character string? I would like to print out >> the name of a function supplied to another function as an argument. >> >> For example: >> >> myFunc = function(x) { x+1 } >> >> applyFunc = function(fnc, x) >> { >> fnc = match.fun(fnc) >> fnc(x) >> } >> >> Is there a way to obtain "myFunc" from the argument fnc in applyFnc >> the following call is issued? >> >> applyFnc(myFunc, 1) > ...or am I missing the basic fact that since arguments to functions in > R are passed by copy, the name is lost/meaningless?You can pass the function name as a string. applyFunc = function(fun, x) { fnc = match.fun(fun) fnc(x) print(fun) } applyFunc("myFunc", 1) [1] "myFunc" PS : avoid renaming the name of your argument within the function ("fnc = match.fun(fnc)"). Cheers, Eloi> > Thanks, > > Peter > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Eloi Mercier Bioinformatics PhD Student, UBC Paul Pavlidis Lab 2185 East Mall University of British Columbia Vancouver BC V6T1Z4
William Dunlap
2012-Aug-29 23:14 UTC
[R] Extracting the name of a function (inverse of match.fun("myFun"))
deparse(substitute(fnc)) will get you part way there.
myFunc <- function(x) x + 1
applyFunc <- function(fnc, x) {
cat("fnc is", deparse(substitute(fnc)), "\n")
fnc <- match.fun(fnc)
fnc(x)
}
> applyFunc(myFunc, 1:3)
fnc is myFunc
[1] 2 3 4> applyFunc(function(x)x*10, 1:3)
fnc is function(x) x * 10
[1] 10 20 30> applyFunc("myFunc", 1:3)
fnc is "myFunc"
[1] 2 3 4
I like to let the user override what substitute might say by
making a new argument out of it:> applyFunc(function(x)x*10, 1:3)
fnc is function(x) x * 10
[1] 10 20 30> applyFunc(function(x)x*10, 1:3, fncString="Times 10")
fnc is Times 10
[1] 10 20 30
This makes is easier to call your function from another one - you
can pass the fncString down through a chain of calls.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at
r-project.org] On Behalf
> Of Peter Langfelder
> Sent: Wednesday, August 29, 2012 4:00 PM
> To: r-help
> Subject: Re: [R] Extracting the name of a function (inverse of
match.fun("myFun"))
>
> On Wed, Aug 29, 2012 at 3:36 PM, Peter Langfelder
> <peter.langfelder at gmail.com> wrote:
> > Hi all,
> >
> > is there a way to extract the name of a function, i.e. do the reverse
> > of match.fun applied to a character string? I would like to print out
> > the name of a function supplied to another function as an argument.
> >
> > For example:
> >
> > myFunc = function(x) { x+1 }
> >
> > applyFunc = function(fnc, x)
> > {
> > fnc = match.fun(fnc)
> > fnc(x)
> > }
> >
> > Is there a way to obtain "myFunc" from the argument fnc in
applyFnc
> > the following call is issued?
> >
> > applyFnc(myFunc, 1)
>
> ...or am I missing the basic fact that since arguments to functions in
> R are passed by copy, the name is lost/meaningless?
>
> Thanks,
>
> Peter
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Rolf Turner
2012-Aug-29 23:29 UTC
[R] Extracting the name of a function (inverse of match.fun("myFun"))
On 30/08/12 10:36, Peter Langfelder wrote:> Hi all, > > is there a way to extract the name of a function, i.e. do the reverse > of match.fun applied to a character string? I would like to print out > the name of a function supplied to another function as an argument. > > For example: > > myFunc = function(x) { x+1 } > > applyFunc = function(fnc, x) > { > fnc = match.fun(fnc) > fnc(x) > } > > Is there a way to obtain "myFunc" from the argument fnc in applyFnc > the following call is issued? > > applyFnc(myFunc, 1)You can just do: applyFunc = function(fnc, x) { fnc(x) } You don't need to get the function's name. That being said, you seem basically to be re-inventing do.call() in a rather kludgy way. I would advise you to think carefully through what you are trying to accomplish. cheers, Rolf Turner P. S. If you really want to get the *name* of the argument "fnc", you can use good old deparse(substitute(...)). As in: fname <- deparse(substitute(fnc)) But as I said, you don't need to do this for what seems to be your purpose, and so it's all rather off the point. R. T.
Peter Langfelder
2012-Aug-29 23:40 UTC
[R] Extracting the name of a function (inverse of match.fun("myFun"))
On Wed, Aug 29, 2012 at 4:14 PM, William Dunlap <wdunlap at tibco.com> wrote:> deparse(substitute(fnc)) will get you part way there....> I like to let the user override what substitute might say by > making a new argument out of it: >> applyFunc(function(x)x*10, 1:3) > fnc is function(x) x * 10 > [1] 10 20 30 >> applyFunc(function(x)x*10, 1:3, fncString="Times 10") > fnc is Times 10 > [1] 10 20 30 > This makes is easier to call your function from another one - you > can pass the fncString down through a chain of calls.Thanks, very good suggestions. The function I am writing is of course much more complicated than the simplistic example. Peter