Parse and evaluate quosures from string

Viewed 640

Is there a way to parse and evaluate a quosure from a string. I would like to achieve the same output as in the example below:

library(rlang)
a <- 10
quo(UQ(a) + 2 * b)
## <quosure: global>
## ~10 + 2 * b

but starting from

t <- "UQ(a) + 2 * b"

What I tried so fare is:

# Trial 1:
quo(expr(t))

# Trial 2: 
parse_quosure(t)

# Trial 3:
quo(parse_quosure(t))
2 Answers

One way would be to use parse to convert t to expression and eval to evaluate it.

eval(parse(text = paste0("quo(",t,")")))
#<quosure: global>
#~10 + 2 * b
Related