Suppose I have an x value that has some (unknown) type (especially a scalar, vector, or list). I would like to get an expression R representing this value. If x == 1 , then this function should simply return expression(1) . For x == c(1,2)) this function should return expression(c(1,2)) . The enquote function enquote pretty close to what I want, but not exactly.
Through some games, I found the following βsolutionβ for my problem:
get_expr <- function(val) { tmp_expr <- enquote(val) tmp_expr[1] <- quote(expression()) return(eval(tmp_expr)) } get_expr(1)
But I think my get_expr function is a kind of hack. Logically, an assessment is not needed.
Is there an even more elegant way to do this? As far as I know, substitute doesn't actually work for me, because the parameter of my get_expr function may be the result of the evaluation (and substitute(eval(expr)) does not perform the evaluation).
I found another way through parse(text = deparse(val)) , but this is an even worse hack ...
source share