Greg Y. answered 03/09/26
CS Degree with 5 Years of STEM Tutoring & 5 Years of Professional R
Quasiquotation
This is a metaprogramming[1] question, where we manipulate R code itself as data. Quasiquotation[2] is a handy way to do this, as shown below.
Here we capture the symbol for the column argument, using the enquo() function[3]. Then we "inject" it into our code, using the special !! operator[4].
A shortcut is to "embrace" it immediately, using the {{ operator[5].
You may even do this with several arguments at once, using the special !!! operator[6].
Your Case
Many tidyverse[7] functions like dplyr[8] support quasiquotation automatically...but most functions like max() do not. When working with the latter, you'll need help from the rlang library[9].
Your best bet is to create your code using the expr() function[10]. Then execute it using the eval_tidy() function[11], which treats the column from your dataset as a regular variable.
The inject() function[12] is an alternative, where you can omit expr(). But this can be tricky when using a dataset like yours, whose columns you wish to treat as variables.
Bibliography
- https://adv-r.hadley.nz/metaprogramming
- https://adv-r.hadley.nz/quasiquotation
- https://rlang.r-lib.org/reference/enquo
- https://rlang.r-lib.org/reference/injection-operator
- https://rlang.r-lib.org/reference/embrace-operator
- https://rlang.r-lib.org/reference/splice-operator
- https://tidyverse.org
- https://dplyr.tidyverse.org
- https://rlang.r-lib.org
- https://rlang.r-lib.org/reference/expr
- https://rlang.r-lib.org/reference/eval_tidy
- https://rlang.r-lib.org/reference/inject