Skip to content

Commit

Permalink
Merge pull request #529 from yjunechoe/tidyselect-lazy-tbl
Browse files Browse the repository at this point in the history
Don't resolve columns with tidyselect when `tbl` cannot be materialized
  • Loading branch information
rich-iannone committed Mar 12, 2024
2 parents a57780f + f720093 commit ade8a31
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions R/utils-tidyselect.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
resolve_columns <- function(x, var_expr, preconditions = NULL, ...,
call = rlang::caller_env()) {

# If columns is just character vector, pass it through
if (rlang::is_character(rlang::quo_get_expr(var_expr))) {
return(rlang::eval_tidy(var_expr))
}

# Materialize table and apply preconditions for tidyselect
tbl <- apply_preconditions_for_cols(x, preconditions)

# If tbl cannot (yet) materialize, don't attempt tidyselect and return early
if (rlang::is_error(tbl)) {
return(resolve_columns_notidyselect(var_expr, tbl, call = call))
}

# Attempt tidyselect
out <- tryCatch(
expr = resolve_columns_internal(tbl, var_expr, ..., call = call),
error = function(cnd) cnd
Expand Down Expand Up @@ -32,16 +44,36 @@ resolve_columns <- function(x, var_expr, preconditions = NULL, ...,

}

resolve_columns_notidyselect <- function(var_expr, parent, call) {
# Error if attempting to tidyselect on a lazy tbl that cannot materialize
var_str <- rlang::expr_deparse(rlang::quo_get_expr(var_expr))
if (any(sapply(names(tidyselect::vars_select_helpers), grepl, var_str))) {
rlang::abort(
"Cannot use tidyselect helpers for an undefined lazy tbl.",
parent = parent,
call = call
)
}

# Force column selection to character vector
if (rlang::quo_is_symbol(var_expr)) {
var_expr <- rlang::as_name(var_expr)
} else if (rlang::quo_is_call(var_expr, c("vars", "c"))) {
var_expr <- rlang::quo_set_expr(var_expr, vars_to_c(var_expr))
}
rlang::eval_tidy(var_expr)
}

# Apply the preconditions function and resolve to data frame for tidyselect
apply_preconditions_for_cols <- function(x, preconditions) {
# Extract tbl
tbl <- if (is_ptblank_agent(x)) {
get_tbl_object(x)
tryCatch(get_tbl_object(x), error = function(cnd) cnd)
} else if (is_a_table_object(x)) {
x
}
# Apply preconditions
if (!is.null(preconditions)) {
if (!rlang::is_error(tbl) && !is.null(preconditions)) {
tbl <- apply_preconditions(tbl = tbl, preconditions = preconditions)
}
tbl
Expand Down

0 comments on commit ade8a31

Please sign in to comment.