Message-ID: <503EA5EE.2020405@xtra.co.nz>
Date: 2012-08-29T23:29:50Z
From: Rolf Turner
Subject: Extracting the name of a function (inverse of match.fun("myFun"))
In-Reply-To: <CA+hbrhUXyWvEHRHO8CD82W2xG5tOihtEZyatz+tv-DzKxdPwOA@mail.gmail.com>
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.