[R] How to get the function names
Duncan Murdoch
murdoch at stats.uwo.ca
Thu Oct 5 23:04:17 CEST 2006
On 10/5/2006 4:41 PM, Søren Højsgaard wrote:
> I've defined the function
>
> getFunNames <- function(FUN){
> if (!is.list(FUN))
> fun.names <- paste(deparse(substitute(FUN)), collapse = " ")
> else
> fun.names <- unlist(lapply(substitute(FUN)[-1], function(a) paste(a)))
> fun.names
> }
>
> which gives what I want :
>> getFunNames(mean)
> [1] "mean"
>> getFunNames(ff)
> [1] "ff"
>> getFunNames(c(mean,ff))
> [1] "mean" "ff"
>
> If I call this within a function, things go wrong:
> 1] "FUN"
>> foo(ff)
> [1] "FUN"
>> foo(c(mean,ff))
> Error in substitute(FUN)[-1] : object is not subsettable
>
> Obviously there are some things (quite a few things) which I have not understood. Can anyone help?
I don't think you'll be able to do what you want. The problem is that R
objects don't know their own name. "substitute" just gives you the
unevaluated expression that was passed as an arg; so getFunNames is
reporting on the expression in foo() that was used when it was called.
You'll need to call substitute directly on the args to get the
expressions, rather than passing them to another function to do that.
In C code you have a bit more flexibility for non-standard evaluation,
but not in R code.
Duncan Murdoch
More information about the R-help
mailing list