Triple exclamation marks on R

Viewed 4071

I've been reading a book on feature engineering, and a piece of code has a triple exclamation mark which I don't understand:

vc_pred <- 
  recipe(Stroke ~ ., data = stroke_train %>% dplyr::select(Stroke, !!!VC_preds)) %>% 
  step_YeoJohnson(all_predictors()) %>% 
  prep(stroke_train %>% dplyr::select(Stroke, !!!VC_preds)) %>% 
  juice() %>% 
  gather(Predictor, value, -Stroke)

VC_preds is a vector containing the variable names of continuous predictors. I understand all the code except by the !!! mark. One ! is supposed to be a negation, but what does it mean !!!?

Any help provided will be greatly appreciated. Thank you.

Regards,

Alexis

1 Answers

!!! is usually used to evaluate a list of expressions.

library(dplyr)
library(rlang)

VC_preds <- c('mpg', 'cyl')
mtcars %>% select(!!!VC_preds) %>% 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

If VC_preds is a vector as in your example, !! should work as well.

mtcars %>% select(!!VC_preds) %>% head

Help page of ?"!!!" gives a better example to understand the difference.

vars <- syms(c("height", "mass"))
vars
#[[1]]
#height

#[[2]]
#mass

starwars %>% select(!!!vars)
# A tibble: 87 x 2
#   height  mass
#    <int> <dbl>
# 1    172    77
# 2    167    75
# 3     96    32
# 4    202   136
# 5    150    49
# 6    178   120
# 7    165    75
# 8     97    32
# 9    183    84
#10    182    77
# … with 77 more rows
Related