2

If I want to get the name of a function (as a string) I can do this:

as.character(quote(mean))
#> [1] "mean"

Or this:

as.character(substitute(mean))
#> [1] "mean"

However this doesn't work if I try to get the function name inside another function.

e.g.

print_fun <- function (fun) {
  function_name <- as.character(quote(fun))
  print(function_name)
}
  print_fun(mean)
#> [1] "fun"

What I am doing wrong?

0

1 Answer 1

2

you just need to substitute quote by substitute.

print_fun <- function (fun) {
  function_name <- as.character(substitute(fun))
  print(function_name)
}
print_fun(mean)

#> [1] "mean"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I was stupid not to try that out I guess. This is what stress does to you.
Thanks, I just learned a bit more about how stackoverflow works :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.