-2

In my R program, I have a function that gets a function as an argument and inside this function I would like to get its name as a string (i.e. the function name of this argument that is supposed to be a function).

frank
  • 481
  • 8
  • 16
  • Please provide a reproducible example (https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – smoff Sep 04 '17 at 09:20
  • Possible duplicate of https://stackoverflow.com/questions/6403852/how-to-call-a-function-using-the-character-string-of-the-function-name-in-r – zx8754 Sep 04 '17 at 09:29

1 Answers1

3

You might be looking for substitute:

f <- function(x) { substitute(x) }

f(mean)

Yields:

mean

which is a symbol. To get it as a string instead, add deparse:

f <- function(x) { deparse(substitute(x)) }

f(mean)

Yields:

[1] "mean"
Axeman
  • 32,068
  • 8
  • 81
  • 94
  • I need the name of the function as a string. I will edit my question accordingly. So I just have to wrap an as.character() around the substitute, right? – frank Sep 04 '17 at 12:37
  • That works, yes. But `?substitute` suggests `deparse(substitute(x))`. – Axeman Sep 04 '17 at 12:40
  • If you add that to your answer, I will mark it as the correct answer. – frank Sep 04 '17 at 16:08