From 7441f5f3dbe44280184e19c48242dc426ca7534b Mon Sep 17 00:00:00 2001 From: June Choe Date: Mon, 11 Mar 2024 17:02:38 -0400 Subject: [PATCH 1/5] safely check if tbl can be materialized at all --- R/utils-tidyselect.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/utils-tidyselect.R b/R/utils-tidyselect.R index bde28e11b..cf1082607 100644 --- a/R/utils-tidyselect.R +++ b/R/utils-tidyselect.R @@ -36,12 +36,12 @@ resolve_columns <- function(x, var_expr, preconditions = NULL, ..., 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(...) NULL) } else if (is_a_table_object(x)) { x } # Apply preconditions - if (!is.null(preconditions)) { + if (!is.null(tbl) && !is.null(preconditions)) { tbl <- apply_preconditions(tbl = tbl, preconditions = preconditions) } tbl From 3a7004f5e1598ebf2a2297055cb06bc3d2dff053 Mon Sep 17 00:00:00 2001 From: June Choe Date: Mon, 11 Mar 2024 17:15:56 -0400 Subject: [PATCH 2/5] resolve columns outside of tidyselect if tbl cannot be materialized --- R/utils-tidyselect.R | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/R/utils-tidyselect.R b/R/utils-tidyselect.R index cf1082607..23a2754b6 100644 --- a/R/utils-tidyselect.R +++ b/R/utils-tidyselect.R @@ -1,8 +1,15 @@ resolve_columns <- function(x, var_expr, preconditions = NULL, ..., call = rlang::caller_env()) { + # 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 (is.null(tbl)) { + return(resolve_columns_notidyselect(var_expr)) + } + + # Attempt tidyselect out <- tryCatch( expr = resolve_columns_internal(tbl, var_expr, ..., call = call), error = function(cnd) cnd @@ -32,6 +39,25 @@ resolve_columns <- function(x, var_expr, preconditions = NULL, ..., } +resolve_columns_notidyselect <- function(var_expr, 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.", + 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 From a9fd9bf9174f5b409f1f77d3b328b1bd5d88baee Mon Sep 17 00:00:00 2001 From: June Choe Date: Mon, 11 Mar 2024 18:03:49 -0400 Subject: [PATCH 3/5] resolve columns outside of tidyselect if possible --- R/utils-tidyselect.R | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/R/utils-tidyselect.R b/R/utils-tidyselect.R index 23a2754b6..39a3679f8 100644 --- a/R/utils-tidyselect.R +++ b/R/utils-tidyselect.R @@ -1,12 +1,17 @@ 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 (is.null(tbl)) { - return(resolve_columns_notidyselect(var_expr)) + return(resolve_columns_notidyselect(var_expr, call = call)) } # Attempt tidyselect From 5ccaacf6f33467e063cc7c501c7431a0ea877284 Mon Sep 17 00:00:00 2001 From: June Choe Date: Mon, 11 Mar 2024 18:15:27 -0400 Subject: [PATCH 4/5] chain error --- R/utils-tidyselect.R | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/R/utils-tidyselect.R b/R/utils-tidyselect.R index 39a3679f8..61536ed94 100644 --- a/R/utils-tidyselect.R +++ b/R/utils-tidyselect.R @@ -10,8 +10,8 @@ resolve_columns <- function(x, var_expr, preconditions = NULL, ..., tbl <- apply_preconditions_for_cols(x, preconditions) # If tbl cannot (yet) materialize, don't attempt tidyselect and return early - if (is.null(tbl)) { - return(resolve_columns_notidyselect(var_expr, call = call)) + if (rlang::is_error(tbl)) { + return(resolve_columns_notidyselect(var_expr, tbl, call = call)) } # Attempt tidyselect @@ -44,12 +44,13 @@ resolve_columns <- function(x, var_expr, preconditions = NULL, ..., } -resolve_columns_notidyselect <- function(var_expr, call) { +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 ) } @@ -67,7 +68,7 @@ resolve_columns_notidyselect <- function(var_expr, call) { apply_preconditions_for_cols <- function(x, preconditions) { # Extract tbl tbl <- if (is_ptblank_agent(x)) { - tryCatch(get_tbl_object(x), error = function(...) NULL) + tryCatch(get_tbl_object(x), error = function(cnd) cnd) } else if (is_a_table_object(x)) { x } From f720093a4237b2cc28c138372d830393d030da4b Mon Sep 17 00:00:00 2001 From: June Choe Date: Mon, 11 Mar 2024 18:25:58 -0400 Subject: [PATCH 5/5] edit check --- R/utils-tidyselect.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils-tidyselect.R b/R/utils-tidyselect.R index 61536ed94..ae61a36d0 100644 --- a/R/utils-tidyselect.R +++ b/R/utils-tidyselect.R @@ -73,7 +73,7 @@ apply_preconditions_for_cols <- function(x, preconditions) { x } # Apply preconditions - if (!is.null(tbl) && !is.null(preconditions)) { + if (!rlang::is_error(tbl) && !is.null(preconditions)) { tbl <- apply_preconditions(tbl = tbl, preconditions = preconditions) } tbl