Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't resolve columns with tidyselect when tbl cannot be materialized #529

Merged
merged 5 commits into from
Mar 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading