Using substitute in multiple function calls

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?

Upvotes: 1

Views: 110

Answers (2)

Saurabh
Saurabh

Reputation: 1626

Here's the rlang approach:

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

Result is:

2 + 2

Upvotes: 1

Roland
Roland

Reputation: 132989

foo1 <- function(parm) {
  p <- substitute(parm)
  do.call(foo, list(p))
}
foo1(2 + 2)
#2 + 2

Upvotes: 1

Related Questions