NSE of an equation in dplyr

Not sure if I gave the correct topic about my question.

Here is the situation I have now

  Combines   Cols
  <chr>      <chr>
1 P_AC       P_A
2 P_AC       P_B
3 P_ABC      P_A
4 P_ABC      P_B
5 P_ABC      P_C

which defines the new columns I want to mutate for my data set, it has columns, such as "P_A, P_B, ...."

I can produce the operation for each new columns I want to add,

configs %>% select(Combines, Cols) %>% 
            group_by(Combines) %>% 
            nest() %>% 
            mutate(eq = map_chr(data, ~paste0(.$Cols,collapse="+"))) %>% 
            ungroup() %>% 
            select(Combines, eq) 

# A tibble: 2 x 3
  Combines       eq         
  <chr>          <chr>      
1 P_AC           P_A+P_C
2 P_ABC        P_A+P_B+P_C

I tried to write the pipe as

mydf %>% mutate(!!sym("P_AC") := eval("P_A+P_C")) -> mydf

It did not work, so the question is for equation here I want to evaluate inside "tidyverse" pipe, what is the correct way.


I did a quick internet search on "rlang equation in mutate" and found a few ideas in some of the Stack Overflow hits. The one that seemed especially relevant was the parse_expr() suggestion in the second part of this answer:

Essentially you replace where you have eval() with rlang::parse_expr().


Great, this is exactly I am looking for.

Thank you, @aosmith

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.