How to parametrize function calls in dplyr 0.7?

dplyr will have a specialized group_by function group_by_at to deal with multiple grouping variables. It would be much easier to use the new member of the _at family: # using the pre-release 0.6.0 cols <- c(“am”,”gear”) mtcars %>% group_by_at(.vars = cols) %>% summarise(mean_cyl=mean(cyl)) # Source: local data frame [4 x 3] # Groups: am [?] … Read more

pass function arguments to both dplyr and ggplot

Tidy evaluation is now fully supported in ggplot2 v3.0.0 so it’s not necessary to use aes_ or aes_string anymore. library(rlang) library(tidyverse) diamond_plot <- function (data, group, metric) { quo_group <- sym(group) quo_metric <- sym(metric) data %>% group_by(!! quo_group) %>% summarise(price = mean(!! quo_metric)) %>% ggplot(aes(x = !! quo_group, y = !! quo_metric)) + geom_col() } … Read more

Use variable names in functions of dplyr

In the newer versions, we can use we can create the variables as quoted and then unquote (UQ or !!) for evaluation var <- quo(color) filter(df, UQ(var) == “blue”) # color value #1 blue 1 #2 blue 3 #3 blue 4 Due to operator precedence, we may require () to wrap around !! filter(df, (!!var) … Read more