[R] from function to its name?
Joerg van den Hoff
j.van_den_hoff at fzd.de
Fri Mar 2 16:50:48 CET 2007
On Fri, Mar 02, 2007 at 03:42:46PM +0100, Ido M. Tamir wrote:
> Hi,
>
> I can get from a string to a function with this name:
>
> >f1 <- function(x){ mean(x) }
>
> >do.call("f1",list{1:4})
> >get("f1")
> etc...
>
> But how do I get from a function to its name?
>
> >funcVec <- c(f1,median)
>
> >funcVec
> [[1]]
> function(x){ mean(x) }
> > str(funcVec)
> List of 2
> $ :function (x)
> ..- attr(*, "source")= chr "function(x){ mean(x) }"
> $ :function (x, ...)
> > deparse(funcVec[1])
> [1] "list(function (x) " "{" " mean(x)"
> [4] "})"
>
for any symbol/name
deparse(substitute(f1))
yields the string representation, but this won't give you "f1" for
deparse(substitute(funcVec[1])).
but rather the string "funcVec[1]".
if you actually want to access funcVec components via strings denoting the
components, maybe you simply could use
funcVec <- list(f1 = f1, median = median)
and access these via
funcVec[["f1"]]
which would enable using a string variable holding the name:
dum = "f1"
funcVec[[dum]]
hth
joerg
More information about the R-help
mailing list