How can I parse and evaluate a column of string expressions in R as part of a pipeline?
In the example below, I produce my desired column, evaluated. But I know this isn't the right approach. I tried taking a tidyverse approach. But I'm just very confused.
library(tidyverse) df <- tibble(name = LETTERS[1:3], to_evaluate = c("1-1+1", "iter+iter", "4*iter-1"), evaluated = NA) iter = 1 for (i in 1:nrow(df)) { df[i,"evaluated"] <- eval(parse(text=df$to_evaluate[[i]])) } print(df) # # A tibble: 3 x 3 # name to_evaluate evaluated # <chr> <chr> <dbl> # 1 A 1-1+1 1 # 2 B iter+iter 2 # 3 C 4*iter-1 3 As part of a pipeline, I tried:
df %>% mutate(evaluated = eval(parse(text=to_evaluate))) df %>% mutate(evaluated = !!parse_exprs(to_evaluate)) df %>% mutate(evaluated = parse_exprs(to_evaluate)) df %>% mutate(evaluated = eval(parse_expr(to_evaluate))) df %>% mutate(evaluated = parse_exprs(to_evaluate)) df %>% mutate(evaluated = eval(parse_exprs(to_evaluate))) df %>% mutate(evaluated = eval_tidy(parse_exprs(to_evaluate))) None of these work.