Search code examples
rtidyeval

tidy eval with e.g. mtcars %>% mutate(target := log(target))


I figured this out while typing my question, but would like to see if there's a cleaner, less code way of doing what I want.

e.g. code block:

target <- "mpg"

# want
mtcars %>% 
  mutate(target := log(target))

I'd like to update mpg to be the log of mpg based on the variable target.

Looks like I got this working with:

mtcars %>% 
  mutate(!! rlang::sym(target) := log(!! rlang::sym(target)))

That just reads as pretty repetitive. Is there a 'cleaner', less code way of achieving the same result?

I'm fond of the double curly braces {{var}}, no reason, they are just nicer to read imho but I couldn't get the same results when I tried:

mtcars %>% 
  mutate(!! rlang::sym(target) := log({{target}}))

What are the various ways I can use tidyeval to mutate a field via transformation based on a pre determined variable to define which field to be transformed, in this case the variable 'target'?


Solution

  • On the lhs of :=, the string can be evaluated with just !!, while on the rhs, it is the value that we need, so we convert to symbol and evaluate (!!)

    library(dplyr)
    mtcars %>% 
         mutate(!!target := log(!! rlang::sym(target)))