0

I would like to write an expression inside mutate in variable and then use it inside mutate. Here is my attempt:

data(mtcars)

newVariables <- "x = hp + gear, y = ((drat + wt) / carb)"

mtcars %>% 
  mutate(newVariables)

How can I use this character vector inside mutate, in a way that it creates 2 new variables?

vestland
  • 55,229
  • 37
  • 187
  • 305
Mislav
  • 1,533
  • 16
  • 37
  • 2
    There are ways to do this, but frankly they would require `eval` and `parse`, and that is generally a bad idea in general, exacerbated significantly within `tidyverse` verbs (due to NSE considerations). – r2evans Mar 08 '19 at 21:44
  • Is it easier to do it using `data.table` or base R? – Mislav Mar 08 '19 at 21:45
  • 4
    It's bad *in general*, and made a little more difficult in `dplyr` and `data.table` due to NSE (which are a convenience to the user). This might be an ["XY problem"](https://meta.stackexchange.com/a/66378/300391), perhaps you can explain the bigger picture from which you think you need to solve it this way? – r2evans Mar 08 '19 at 21:49
  • 1
    Working with strings is not a great idea. You could work with actual expressions `newVariables <- rlang::exprs(x = hp + gear, y = ((drat + wt) / carb)); mtcars %>% mutate(!!!newVariables)` – MrFlick Mar 08 '19 at 21:50
  • It's bad in general because: (1) performance, as it is often much much slower than "regular methods"; (2) analogs of [SQL injection](https://stackoverflow.com/q/332365/3358272); (3) typically much harder to debug **when** things go wrong. – r2evans Mar 08 '19 at 21:51
  • In nutshell, I have formula class, which I want to use innside lm, and everything is interactive inside Shiny app. User can choose formula. Formula can contain ratios (x / y) which can produce Inf values. So before lm, I wan to remove Inf values. To remove Inf values I have to create arbitrary ratio (say x /y ) that i one of the terms in the formula and than remove Inf values. – Mislav Mar 08 '19 at 21:53
  • 2
    Okay, we're getting closer. As MrFlick clarified, working with strings like this has its problems. Could you edit your question with a minimal app (even not shiny if you can mimic enough of the part that is user-chosen). BTW: creating a formula from strings is much better (though not perfect) than parsing/evaluating code, so that might be a better way to go. – r2evans Mar 08 '19 at 21:55
  • I tried MrFlick solution, but didn't work. I created one column with repetitive hp + gear part. I will try to update question tomorrow. – Mislav Mar 08 '19 at 22:12
  • 1
    You should avoid parsing string, specially if you are making shiny input for such string. [Here](https://github.com/Rdatatable/data.table/issues/2655#issuecomment-376781159) is presented why. You can check this question for possible options you have to solve your problem: https://stackoverflow.com/questions/24833247/how-can-one-work-fully-generically-in-data-table-in-r-with-column-names-in-varia – jangorecki Mar 09 '19 at 15:07

0 Answers0