0

While passing in a function into another function, I want the string name of this function. I also want this to work with the {magrittr} pipe operator.

This is very similar to this question. But when I try it with the pipe, it fails.

library(magrittr)

h <- function(func) {
  as.character(substitute(func))
}

# Works
h(eval)
#> [1] "eval"

# Doesn't work
eval() %>% h()
#> [1] "."

# Works
eval() %>% substitute() %>% as.character()
#> [1] "eval"

Created on 2021-08-04 by the reprex package (v2.0.0)

What I'm expecting is:

eval() %>% h()
#> [1] "eval"

Can this be done with a single function call and not a chain call like for the last example? I'm using {magrittr} version 2.0.1.

Eric Leung
  • 2,354
  • 12
  • 25
  • 1
    You can probably adapt the solution from a [similar question](https://stackoverflow.com/questions/52066097/get-expression-that-evaluated-to-dot-in-function-called-by-magrittr-pipe) and convert the output to a string. – Artem Sokolov Aug 11 '21 at 18:35
  • Thanks! This is perfect. – Eric Leung Aug 12 '21 at 14:35

0 Answers0