1
foo <- function(arg) {
  substitute(arg)
}

foo1 <- function(parm) {
  foo(param)
}
foo1(2 + 2)

output is:

param

How can I use substitute inside foo such that the output will be the expression 2 + 2?

2 Answers2

1

Here's the rlang approach:

foo <- function(...) {
  x <- rlang::enexprs(...)
  x[[1]]
}
foo(2 + 2)

Result is:

2 + 2
Saurabh
  • 1,566
  • 10
  • 23
1
foo1 <- function(parm) {
  p <- substitute(parm)
  do.call(foo, list(p))
}
foo1(2 + 2)
#2 + 2
Roland
  • 127,288
  • 10
  • 191
  • 288