Tidy evaluation in 5 mins

Hadley Wickham · 26K views · 2018 · 5:59

Open on YouTube

Data science & statisticsPython, Ruby & scripting languagesexplainerdated

Why Stack Overflow recommends it

Stack Overflow users link it when explaining enquo(), quosures and !! to people writing dplyr or ggplot2 wrapper functions who don't yet grasp tidy evaluation.

Recommended by 3 people across 4 different months, 2018 to 2019.

What it is

A six-minute talk by Hadley Wickham that walks through the five core ideas behind tidy evaluation in R's tidyverse, including quosures and quasiquotation. It is meant as a big-picture overview before reading the deeper treatment in Advanced R.

What people said

Recommends Tung · 2018-03 · Stack Overflow · on “Use highcharter inside a function”

We need to use enquo() & quo_name() together with hcaes_string enquo() captures the expression supplied as argument by the user & returns a quosure. quo_name() squashes the quosure and transforms it into a string. Watch Hadley explained tidyeval in this 5min video [this video] if you haven't heard about it. More on tidyeval here [LINK] library(tidyverse) library(lubridate) library(highcharter) library(nycflights13) flights_2 <- flights %>% mutate(dep_mo = ymd(str_c(year, month, "01", sep = "-"))) %>% group_by(dep_mo) %>% summarize(arr_delay = mean(arr_delay, na.rm = TRUE)) h_fun2 <- function(df, x, y) { x <- enquo(x) y <- enquo(y) hchart(df, type = "line", hcaes_string(quo_name(x), quo_name(y)),

Recommends Tung · 2018-08 · Stack Overflow · on “Non-standard evaluation and quasiquotation in dplyr() not working as (naively) expected”

@Josh: what is `count_coln`? `enquo` is usually used inside a function. I suggest you watch Hadley's 5min tidy evaluation video [this video]

Recommends M_M · 2018-09 · Stack Overflow · on “Using a custom function with tidyverse”

Thanks Abdallah! Quoting from the link you shared "enquo() uses some dark magic to look at the argument, see what the user typed, and return that value as a quosure." Also, I found this to be useful too: [this video]

Recommends mgiormenti · 2019-04 · Stack Overflow · on “How to plot a large number of density plots with different categorical variables”

This solutions gives all the plots in a list. Here we make a single function that accepts a variable that you want to plot, and then use lapply with a vector of all the variables you want to plot. fill_variables <- vars(carrier, origin) func_plot <- function(fill_variable) { dat %>% ggplot(aes(x = distance, fill = !!fill_variable)) + geom_density() } plotlist <- lapply(fill_variables, func_plot) If you have no idea of what those !! mean, I recommend watching this 5 minute video that introduces the key concepts of tidy evaluation [this video] . This is what you want to use when you want to create this sorts of wrapper functions to do stuff programmatically. I hope this helps! Edit: If you want to

Cites Tung · 2019-11 · Stack Overflow · on “Error when using dplyr inside of a function”

If quosure and quasiquotation are too much for you, use either .data[[ ]] or rlang {{ }} ( curly curly [LINK] ) instead. See Hadley Wickham's 5min video on tidy evaluation [this video] and (maybe) Tidy evaluation section [LINK] in Hadley's Advanced R book for more information. library(rlang) library(dplyr) filter_data <- function(df, spp, LENGTH, WIDTH) { res <- df %>% filter(Species == spp) %>% select(.data[[LENGTH]], .data[[WIDTH]]) %>% mutate(sum = .data[[LENGTH]] + .data[[WIDTH]]) %>% filter(sum > 4) %>% nrow() return(res) } filter_data(iris, "virginica", "Sepal.Length", "Sepal.Width") #> [1] 50 filter_rlang <- function(df, spp, LENGTH, WIDTH) { res <- df %>% filter(Species == spp) %>% select