Skip to content

Commit

Permalink
Use ensyms() rather than quos() to capture variables
Browse files Browse the repository at this point in the history
The latter is problematic because it allows arbitrary expressions.
These variables are forwarded to select(), so potential expressions
are `starts_with()`, `one_of()`, etc.  The naniar code generally
assumes that only symbols are passed in dots. `ensyms()` is a way of
ensuring the input types.
  • Loading branch information
lionel- committed Oct 6, 2018
1 parent e7b2b88 commit 3e0552b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
2 changes: 1 addition & 1 deletion R/shadows.R
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ shadow_long <- function(shadow_data,
}

if (!missing(...)) {
vars <- bare_to_chr(...)
vars <- purrr::map(ensyms(...), as_string)
gathered_df <- gathered_df %>%
dplyr::filter(variable %in% vars)
}
Expand Down
23 changes: 9 additions & 14 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,24 +195,19 @@ add_span_counter <- function(data, span_size) {
#' @return a list containing the levels of everything
what_levels <- function(x) purrr::map(x, levels)

# utility function to convert bare name to character
bare_to_chr <- function(...){
ps <- rlang::exprs(...)

exprs_text <- function(ps) {
paste0(purrr::map_chr(ps, rlang::expr_text))
}

exprs_text(ps)

}

quo_to_shade <- function(...){

quo_vars <- rlang::quos(...)
# Use ensyms() rather than quos() because the latter allows
# arbitrary expressions. These variables are forwarded to select(),
# so potential expressions are `starts_with()`, `one_of()`, etc.
# The naniar code generally assumes that only symbols are passed in
# dots. `ensyms()` is a way of ensuring the input types.
vars <- rlang::ensyms(...)

shadow_chr <- bare_to_chr(!!!quo_vars) %>% paste0("_NA")
# Adding `_NA` suffix to user symbols
shadow_chr <- purrr::map(vars, as_string) %>% paste0("_NA")

# Casting back to symbols
shadow_vars <- rlang::syms(shadow_chr)

return(shadow_vars)
Expand Down

0 comments on commit 3e0552b

Please sign in to comment.