Why Tidy Eval How it works • Delay computations by quoting • Change the context and resume computation starwars %>% filter( height < 200, gender == "male" )
Two flavours starwars %>% mutate(birth_year - 100) starwars %>% group_by(birth_year) starwars %>% select(birth_year) starwars %>% filter(birth_year < 50) One of these things is not like the other things!
Two flavours starwars %>% mutate(birth_year - 100) starwars %>% group_by(birth_year) starwars %>% select(birth_year) starwars %>% filter(birth_year < 50) One of these things is not like the other things! Action Selection
tmp <- starwars$birth_year - 100 starwars$`birth_year - 100` <- tmp starwars %>% mutate(birth_year - 100) Most verbs take actions 1. New vectors are created 2. The data frame is modified
Some verbs take selections 1. The position of columns is looked up 2. The data frame is reorganised starwars %>% select(birth_year) tmp <- match("birth_year", colnames(starwars)) starwars[, tmp]
starwars %>% select(ends_with("color")) starwars %>% select(matches("^[nm]a") starwars %>% select(10, everything()) 1. c(), `-` and `:` understand positions and names 2. Selection helpers know about current variables Selections have special properties
Three challenges 1. Taking user selections or actions like a tidy eval function 2. Modifying these selections or actions 3. Passing selections or actions to other tidy eval functions
Three challenges 1. Taking user selections or actions like a tidy eval function 2. Modifying these selections or actions 3. Passing selections or actions to other tidy eval functions
Three challenges 1. Taking user selections or actions like a tidy eval function 2. Modifying these selections or actions 3. Passing selections or actions to other tidy eval functions
• Tidy eval, the easy way • Make use of existing components • Solves challenges 1 and 3 • Limited but useful! my_component <- function(.data, ...) { .data %>% summarise(...) }
Three examples 1. Create a selection verb with dplyr 2. Transform that verb to take actions instead 3. Add tidyr step to the pipeline Pass the dots ... !
• Most dplyr verbs have variants suffixed with _at • They take selections within vars(...) • mutate_at() and summarise_at() apply a function on each of those vars 1. Selections with dplyr mutate_at() summarise_at() filter_at() rename_at() arrange_at()
• How could we pass actions instead of selections? • In dplyr, transmute() is the fundamental action verb • Returns as many columns as supplied actions 2. Actions with dplyr
• What if we'd like to gather results across rows? • Let's develop the pipeline with a tidyr step • Handling groups will be trickier 3. Gather with tidyr
starwars %>% group_by(gender) %>% gather_summarise_acts( heightm = height / 100, bmi = mass / heightm^2 ) Warning messages: 1: In mean.default(Value, na.rm = TRUE) : argument is not numeric or logical: returning NA 2: In mean.default(Value, na.rm = TRUE) : argument is not numeric or logical: returning NA 3: In mean.default(Value, na.rm = TRUE) : argument is not numeric or logical: returning NA • gather() also gathers grouping variables • Summaries can't be applied on character
• Pass dots to create tidy eval functions easily • Do you need actions or selections? • The _at variants and transmute() are useful • Requires knowledge of tidyverse verbs — transferable • Think about grouped tibbles summary()