Quosure

A quosure is a quoted expression that keeps track of the environment it’s in. For example it can be used in code to access parts of objects that exist within the environment used to evaluate that code, i.e. selecting columns from a data frame which has been piped into select.

We have a data frame called “transport_data” and it has 13 columns shown below. We want to select the column “number.of.items”, using quosure.

names(transport_data)
##  [1] "sender.country"       "sender.city"          "sender.state"        
##  [4] "sender.latitude"      "sender.longitude"     "receiver.country"    
##  [7] "receiver.city"        "receiver.state"       "receiver.latitude"   
## [10] "receiver.longitude"   "date"                 "number.of.items"     
## [13] "percent.of.all.items"
measure_col_naked <- quo(number.of.items)

Quosi quotation

We need to tell the select statement to treat the object measure_col_naked as a quosure. To do this we need to do what’s called quasi quotation of an expression. This un-quotes a quosure allowing it to be evaluated within the context that the code appears. The operator of unquoting your quosure is a double quotation mark.

transport_data %>%
  select(!!measure_col_naked)
## # A tibble: 112 x 1
##    number.of.items
##              <dbl>
##  1             381
##  2             372
##  3             255
##  4             351
##  5             492
##  6             347
##  7             462
##  8             500
##  9             414
## 10             426
## # ... with 102 more rows

Alternatively, we can use select(UQ) for unquoting a quosure and then the quosure, so measure_col_naked.

transport_data %>% 
  select(UQ(measure_col_naked))
## # A tibble: 112 x 1
##    number.of.items
##              <dbl>
##  1             381
##  2             372
##  3             255
##  4             351
##  5             492
##  6             347
##  7             462
##  8             500
##  9             414
## 10             426
## # ... with 102 more rows

Quosure containing multiple variables

we have a variable called useful_columns_naked which we attempt to build using a vector so what we do is we specify this instead of as a vector, as a quos, so as a quosure containing multiple variables.

useful_columns_naked <- quos(date, receiver.city, number.of.items)

Unquote quos

If you have multiple arguments within a quosure, you need to use three exclamation marks to unquote.

transport_data %>%
  select(!!!useful_columns_naked)
## # A tibble: 112 x 3
##    date       receiver.city number.of.items
##    <date>     <chr>                   <dbl>
##  1 1884-08-10 Limbach                   381
##  2 1885-07-01 Limbach                   372
##  3 1885-11-14 Limbach                   255
##  4 1877-03-14 Limbach                   351
##  5 1880-02-04 Limbach                   492
##  6 1880-08-19 Limbach                   347
##  7 1880-12-05 Limbach                   462
##  8 1881-05-11 Limbach                   500
##  9 1881-08-03 Limbach                   414
## 10 1882-02-08 Limbach                   426
## # ... with 102 more rows

Reference

Learning the r tidyverse course by Charlie Joey Hadley

https://www.linkedin.com/learning/learning-the-r-tidyverse/