Message-ID: <503EA0DB.8090100@chibi.ubc.ca>
Date: 2012-08-29T23:08:11Z
From: Eloi Mercier
Subject: Extracting the name of a function (inverse of match.fun("myFun"))
In-Reply-To: <CA+hbrhU8kf-aanj4L-6XyXPSSxZZmiX+uL_7=rKQtPfFcd_m0w@mail.gmail.com>
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