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).
Asked
Active
Viewed 249 times
-2
-
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 Answers
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
-
-