Getting a function name as a string
I was wanting the same thing, and remembered library(foo)
didn't need quotes, this is what it does:
package <- as.character(substitute(package))
Another approach would be to pass the names of the functions into your report function, and then get the functions themselves with the get()
command. For instance:
function.names <- c("which","all")
fun1 <- get(function.names[1])
fun2 <- get(function.names[2])
Then you have the names in your original character vector, and the functions have new names as you defined them. In this case, the all
function is now being called as fun2
:
> fun2(c(TRUE, FALSE))
[1] FALSE
Or, if you really want to keep the original function names, just assign them locally with the assign function:
assign(function.names[2], get(function.names[2]))
If you run this command right now, you will end up with the all
function in your ".GlobalEnv"
. You can see this with ls()
.
That may lead to parse(eval(...))
at which point you are open for this critique:
R> library(fortunes)
R> fortune("parse")
If the answer is parse() you should usually rethink the question.
-- Thomas Lumley
R-help (February 2005)
R>
So do your functions have to be called MyFunction.1
etc pp?