Reputation: 5131
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
Reputation: 1626
Here's the rlang approach:
foo <- function(...) {
x <- rlang::enexprs(...)
x[[1]]
}
foo(2 + 2)
Result is:
2 + 2
Upvotes: 1
Reputation: 132989
foo1 <- function(parm) {
p <- substitute(parm)
do.call(foo, list(p))
}
foo1(2 + 2)
#2 + 2
Upvotes: 1