Getting a function name as a string

RFunction

R Problem Overview


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?

R Solutions


Solution 1 - R

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

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

Solution 2 - R

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

Solution 3 - R

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?

Solution 4 - R

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

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

Solution 5 - R

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.

Solution 6 - R

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"

Solution 7 - R

what about this:

deparse(quote(foo.bar))

Solution 8 - R

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!

Solution 9 - R

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHamiltonUlmerView Question on Stackoverflow
Solution 1 - RnfultzView Answer on Stackoverflow
Solution 2 - RShaneView Answer on Stackoverflow
Solution 3 - RDirk EddelbuettelView Answer on Stackoverflow
Solution 4 - RJonathan ChangView Answer on Stackoverflow
Solution 5 - RlionelcView Answer on Stackoverflow
Solution 6 - RHarlanView Answer on Stackoverflow
Solution 7 - RmariotomoView Answer on Stackoverflow
Solution 8 - Ruser11422223View Answer on Stackoverflow
Solution 9 - RDomingoView Answer on Stackoverflow