B.4 Non-standard Evaluation (NSE)

B.4.1 Tidy evaluation

https://stackoverflow.com/questions/57136322/what-does-the-operator-mean-in-r-particularly-in-the-context-symx https://www.youtube.com/watch?v=nERXS3ssntw https://forum.posit.co/t/how-to-properly-and-dynamically-use-variables-in-dplyr-pipelines/156720/4 https://www.r-bloggers.com/2019/07/bang-bang-how-to-program-with-dplyr/

library(dplyr)

var <- "mpg"
mtcars %>%
  select(!!sym(var)) %>%
  head()
##                    mpg
## Mazda RX4         21.0
## Mazda RX4 Wag     21.0
## Datsun 710        22.8
## Hornet 4 Drive    21.4
## Hornet Sportabout 18.7
## Valiant           18.1
vars <- c("mpg", "cyl")
mtcars %>%
  select(!!!syms(vars)) %>%
  head()
##                    mpg cyl
## Mazda RX4         21.0   6
## Mazda RX4 Wag     21.0   6
## Datsun 710        22.8   4
## Hornet 4 Drive    21.4   6
## Hornet Sportabout 18.7   8
## Valiant           18.1   6