1

hopefully this is not too simple. I am looking for a function which gives me the name of a function, which I assigned to a variable. So something like

x <- mean
the_function_i_look_for(x)
[1] "mean"

Any ideas? Many thanks in advance!

Edit:

Ok, this is a more detailed example: Actually I have a function where I pass an arbitrary logarithm and which gives me a data.frame back. One column name of the data frame should indicate which logarithm was used.

> myFunction <- function( log, x ) {
    df <- data.frame( x, log(x))
    names(df) <- c(the_function_i_look_for(log), "x")
    return(df)
  }
> myFunction( log10, c(10,100,1000) )
>   log10    x
  1     1   10
  2     2  100
  3     3 1000
Beasterfield
  • 7,023
  • 2
  • 38
  • 47
  • 2
    What are you actually trying to do? Depending on what you intend to do with your `x` , there are different approaches. You may well want to store only the function name as a string, for example – Carl Witthoft May 15 '12 at 13:41
  • 1
    is the function (you use the example `mean`) an argument to your function? I think it would be best if you actually added some of the code to show use how you're using it. – Tyler Rinker May 15 '12 at 13:47
  • `methods(x)` will tell you if you have any methods defined for this function. If true, you will see function name, followed by a method. Now to come up with a solution for functions without defined methods... :) – Roman Luštrik May 15 '12 at 13:50
  • Maybe this question could be helpful: http://stackoverflow.com/questions/9638372/how-to-compare-functions – sgibb May 15 '12 at 13:56
  • @sgibb sorry, it is not. – Beasterfield May 15 '12 at 14:05
  • @RomanLuštrik doesn't help either – Beasterfield May 15 '12 at 14:06

1 Answers1

3

You could use ?substitute:

myFunction <- function( log, x ) {
    df <- data.frame( x, log(x))
    names(df) <- c("x", substitute(log))
    return(df)
}

myFunction( log10, c(10,100,1000) )
#     x log10
#1   10     1
#2  100     2
#3 1000     3

EDIT: Seems to work for ReferenceClasses, too:

foo <- setRefClass("foo",
    methods = list(
    bar = function(x) {
        message("function: ", substitute(x))
    }
))

f <- foo$new()
f$bar(log)
# function: log
f$bar(mean)
# function: mean
sgibb
  • 25,396
  • 3
  • 68
  • 74
  • Maybe, I was a little bit too fast accepting this answer and must really admit, that I was not aware how complicated R is in this regard. `myFunction` is actually not just a function, but a method of a ReferenceClass. The solution using `substitute` (without specifying an environment) does not seem to work there. – Beasterfield May 15 '12 at 15:57
  • 1
    well, can you edit your question to give us a reproducible example then? (PS, just curious: in what language is this task *easy*?) – Ben Bolker May 15 '12 at 17:26
  • 1
    @Isam your last comment is not clear, but maybe deparse(substitute(f)) instead of substitute(f) is the way you're looking for – Stéphane Laurent May 15 '12 at 19:42
  • @sgibb I am really sorry, but there must have been some other reason why it didn't work. However, everything is fine now! Many thanks again. – Beasterfield May 16 '12 at 08:58
  • @BenBolker I am not so deeply experienced with many programming languages. But using Reflections in Java for example would not only have been more straight forward but more consistent to use as well. – Beasterfield May 16 '12 at 09:00