Skip to content

Instantly share code, notes, and snippets.

@hypercompetent
Last active July 14, 2019 05:18
Show Gist options
  • Save hypercompetent/cad03dfeca5582ab78250b475e2364d0 to your computer and use it in GitHub Desktop.
Save hypercompetent/cad03dfeca5582ab78250b475e2364d0 to your computer and use it in GitHub Desktop.
Notes for tidy evaluation based on strings as sources of column names
# Tidyeval notes
library(dplyr)
library(rlang)
library(tasic2016data)
# one character element:
test <- "primary_type_id"
expr_test <- parse_expr(test)
tasic_2016_anno %>%
group_by(!!expr_test) %>%
summarise(x = n())
# or
tasic_2016_anno %>%
group_by(!!parse_expr(test)) %>%
summarise(x = n())
# one or more character elements
test <- c("primary_type_id","secondary_type_id")
expr_test <- lapply(test, parse_expr)
tasic_2016_anno %>%
group_by(!!!expr_test) %>%
summarise(x = n())
# a single entry from multiple elements
test <- c("primary_type_id","secondary_type_id")
expr_test <- lapply(test, parse_expr)
tasic_2016_anno %>%
group_by(!!expr_test[[1]]) %>%
summarise(x = n())
# Filtering:
test <- "primary_type_id"
test_targets <- c(1:5)
tasic_2016_anno %>%
filter(!!parse_expr(test) %in% test_targets) %>%
group_by(!!parse_expr(test)) %>%
summarise(x = n())
# or
expr_test <- parse_expr(test)
tasic_2016_anno %>%
filter(!!expr_test %in% test_targets) %>%
group_by(!!expr_test) %>%
summarise(x = n())
@hypercompetent
Copy link
Author

Note: the tasic2016data package can be installed with:

devtools::install_github("AllenInstitute/tasic2016data")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment