HamiltonUlmer
HamiltonUlmer

Reputation: 2459

Getting a function name as a string

Say I have a bunch of functions, each with something likeMyFunction.1, etc. I want to pass these functions into another function, which prints out a small report. Ideally I'd like to be able to label sections of a report by which function is being used to generate the results.

So are there any nice ways of getting the name of a predefined function as a string?

Upvotes: 70

Views: 42350

Answers (10)

Sermet Pekin
Sermet Pekin

Reputation: 355

In my experience rlang::ensyms function leads a concise solution and works best.

library(rlang)
get_func_name <- function(  ...  ){

     name <- map(rlang::ensyms(  ... ) , as.character) 
                
     unlist(name)
}
    
    
    get_func_name(str)
    #"str"
    
    #even itself :)
    get_func_name(str , formatC , get_func_name)
    # "str"       "formatC" "get_func_name" 
    #

Upvotes: 0

Domingo
Domingo

Reputation: 671

I also struggled a lot with this issue and unfortunately the given solutions were not working out for me, but I came up with a way to get the name with R6 classes.

Here a minimal example:

NameProvider <-
  R6::R6Class(
    "NameProvider",
    base::list(
      get_fun_name = function(fun_name) { return(fun_name)})
    )

NameUser <-
  R6::R6Class(
    "NameUser",
    base::list(
      do_something = function(fun)
        {
          np <- NameProvider$new()
          fname <- np$get_fun_name(fun_name = base::as.character(base::substitute(fun)))
          do_the_thing <- paste0("THIS IS ", base::toupper(fname), "!!!")
          return(do_the_thing)
        }
        )
      )

nu <- NameUser$new()
nu$do_something(fun = mean) 

The class NameProvider just returns the name as a string and in the class NameUser the call for base::as.character(base::substitute(...)) is made.

Upvotes: 0

user11422223
user11422223

Reputation:

Nice observation by @nfultz, so the answer to this thread's question would be :-

workingFunction <- Function(f)
{ functionName <- as.character(substitute(f)) }

Or

workingFunction <- Function(f)
{ functionName <- deparse(substitute(f)) }

All the other answers would simply return the parameter name itself ('f' in the example above) - I tried them all since I've been working on a function and experienced this issue of not being able to retrieve a function's name inside another function wherein the first is passed as parameter to the second function while calling it. Hope my answer helps to all those who might be stuck for the same!

Upvotes: 3

lionelc
lionelc

Reputation: 51

Just want to provide an example to show the advantage and limitation in this issue:

I want to "save" a function with its name, as an option that would be used in another function:

R> foreach(..., .combine=test_fun) {...}

The test_fun is the name of the function, and of course

R> mode(test_fun)  
[1] "function"

When I use it in foreach, I just need the function name, while test_fun can be an existing function (e.g. cbind). So, test_fun is assigned by

R> test_fun <- get('cbind')

or

R> test_fun <- assign('cbind', get('cbind'))

So, you got the function here

R> test_fun  
function (..., deparse.level = 1)   
.Internal(cbind(deparse.level, ...))  

actually, the original name can't be maintained, so you have no way to convert test_fun back to string "cbind".

R> deparse(substitute(test_fun))  
[1] "test_fun"

I unfortunately need to deparse the foreach code so want the original name shown in the string. This means that the only way is to save 'cbind' as a string and creating such a function object brings no benefit in this case.

Upvotes: 4

nfultz
nfultz

Reputation: 731

I was wanting the same thing, and remembered library(foo) didn't need quotes, this is what it does:

package <- as.character(substitute(package))

Upvotes: 73

mariotomo
mariotomo

Reputation: 9748

what about this:

deparse(quote(foo.bar))

Upvotes: 3

Jonathan Chang
Jonathan Chang

Reputation: 25367

You can get the unevaluated arguments of a function via match.call. For example:

> x <- function(y) print(match.call()[2])
> x(lm)
lm()

Upvotes: 8

Shane
Shane

Reputation: 100194

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().

Upvotes: 28

Harlan
Harlan

Reputation: 19401

When a function is passed around as an object, it loses its name. See, for example, the results of the following lines:

str(lm)
lm

You can get the arguments and the body of the function, but not the name.

My suggestion would be to construct a named list of functions, where the name could be printed:

> somefns <- list(lm=lm, aggregate=aggregate)
> str(somefns)
List of 2
 $ lm       :function (formula, data, subset, weights, na.action, method = "qr", 
    model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, 
    contrasts = NULL, offset, ...)  
 $ aggregate:function (x, ...) 

> somefns[[1]](dist ~ speed, data=cars)

Call:
somefns[[1]](formula = dist ~ speed, data = cars)

Coefficients:
(Intercept)        speed  
     -17.58         3.93  

> names(somefns)[[1]]
[1] "lm"

Upvotes: 4

Dirk is no longer here
Dirk is no longer here

Reputation: 368519

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?

Upvotes: 10

Related Questions