I am in the habit of passing character vectors to group_by_ and other functions so I can programmatically generate custom reports using dplyr, based on input from a file or a Shiny UI form. but this is not so straightforward in dplyr 0.6. In this SO question I raised the issue and the brilliant MrFlick came up with the solution below.
Does it make sense to export as_quosure or a similar convenience function to help people like me program with dplyr more easily? Programmers must be able to use dplyr with string column names in order to have robust interoperation with other systems (e.g. reading column names from files, from Shiny UI widgets, etc.)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
# Programming dplyr with quosure inputs
grouping_vars <- quos(am, gear)
mtcars %>%
group_by(!!!grouping_vars) %>%
summarise(mean_cyl=mean(cyl))
#> Source: local data frame [4 x 3]
#> Groups: am [?]
#>
#> am gear mean_cyl
#> <dbl> <dbl> <dbl>
#> 1 0 3 7.466667
#> 2 0 4 5.000000
#> 3 1 4 4.500000
#> 4 1 5 6.000000
# Programming dplyr with string inputs
as_quosure <- function(strs) rlang::parse_quosures(paste(strs, collapse=";"))
cols <- c("am", "gear")
mtcars %>%
group_by(!!!as_quosure(cols)) %>%
summarise(mean_cyl=mean(cyl))
#> Source: local data frame [4 x 3]
#> Groups: am [?]
#>
#> am gear mean_cyl
#> <dbl> <dbl> <dbl>
#> 1 0 3 7.466667
#> 2 0 4 5.000000
#> 3 1 4 4.500000
#> 4 1 5 6.000000
Closing as duplicate of #2662
I can categorically say if you're pasting strings to program with dplyr, there is always better way.
Most helpful comment
I can categorically say if you're pasting strings to program with dplyr, there is always better way.