From b55408284c734198bfc3cf37bf42979ef7868705 Mon Sep 17 00:00:00 2001 From: olivroy Date: Wed, 23 Oct 2024 11:14:06 -0400 Subject: [PATCH 1/4] Use `error_arg` where appropriate --- R/drop-na.R | 2 +- R/fill.R | 3 ++- R/pack.R | 1 + R/pivot-long.R | 6 ++---- R/pivot-wide.R | 7 ++++++- R/separate-longer.R | 1 + R/separate-rows.R | 7 ++++++- R/separate-wider.R | 1 + R/unite.R | 2 +- R/unnest-longer.R | 2 +- R/unnest-wider.R | 2 +- R/unnest.R | 3 ++- 12 files changed, 25 insertions(+), 12 deletions(-) diff --git a/R/drop-na.R b/R/drop-na.R index d5f9c83ea..49721b76c 100644 --- a/R/drop-na.R +++ b/R/drop-na.R @@ -32,7 +32,7 @@ drop_na.data.frame <- function(data, ...) { # Use all columns if no `...` are supplied cols <- data } else { - vars <- tidyselect::eval_select(expr(c(!!!dots)), data, allow_rename = FALSE) + vars <- tidyselect::eval_select(expr(c(!!!dots)), data, allow_rename = FALSE, error_arg = "...") cols <- data[vars] } diff --git a/R/fill.R b/R/fill.R index 07b920549..03afb04b4 100644 --- a/R/fill.R +++ b/R/fill.R @@ -114,7 +114,8 @@ fill.data.frame <- function(data, vars <- names(tidyselect::eval_select( expr = expr(c(...)), data = data, - allow_rename = FALSE + allow_rename = FALSE, + error_arg = "..." )) .direction <- arg_match0( diff --git a/R/pack.R b/R/pack.R index 1aca68d09..7a44f588d 100644 --- a/R/pack.R +++ b/R/pack.R @@ -134,6 +134,7 @@ unpack <- function(data, expr = enquo(cols), data = data, allow_rename = FALSE, + error_arg = "cols", error_call = error_call ) cols <- out[cols] diff --git a/R/pivot-long.R b/R/pivot-long.R index ebece08d8..341989116 100644 --- a/R/pivot-long.R +++ b/R/pivot-long.R @@ -364,14 +364,12 @@ build_longer_spec <- function(data, expr = enquo(cols), data = data[unique(names(data))], allow_rename = FALSE, + allow_empty = FALSE, + error_arg = "cols", error_call = error_call ) cols <- names(cols) - if (length(cols) == 0) { - cli::cli_abort("{.arg cols} must select at least one column.", call = error_call) - } - if (is.null(names_prefix)) { names <- cols } else { diff --git a/R/pivot-wide.R b/R/pivot-wide.R index 2ae59bbd9..2122b0f93 100644 --- a/R/pivot-wide.R +++ b/R/pivot-wide.R @@ -486,6 +486,7 @@ build_wider_spec <- function(data, data, allow_rename = FALSE, allow_empty = FALSE, + error_arg = "names_from", error_call = error_call ) values_from <- tidyselect::eval_select( @@ -493,6 +494,7 @@ build_wider_spec <- function(data, data, allow_rename = FALSE, allow_empty = FALSE, + error_arg = "values_from", error_call = error_call ) @@ -562,6 +564,7 @@ build_wider_id_cols_expr <- function(data, enquo(names_from), data, allow_rename = FALSE, + error_arg = "names_from", error_call = error_call ) @@ -569,6 +572,7 @@ build_wider_id_cols_expr <- function(data, enquo(values_from), data, allow_rename = FALSE, + error_arg = "values_from", error_call = error_call ) @@ -603,6 +607,7 @@ select_wider_id_cols <- function(data, enquo(id_cols), data, allow_rename = FALSE, + error_arg = "id_cols", error_call = error_call ), vctrs_error_subscript_oob = function(cnd) { @@ -631,7 +636,7 @@ rethrow_id_cols_oob <- function(cnd, names_from_cols, values_from_cols, call) { stop_id_cols_oob <- function(i, arg, call) { cli::cli_abort( c( - "`id_cols` can't select a column already selected by `{arg}`.", + "{.arg id_cols} can't select a column already selected by `{arg}`.", i = "Column `{i}` has already been selected." ), parent = NA, diff --git a/R/separate-longer.R b/R/separate-longer.R index dd5830124..70014fdba 100644 --- a/R/separate-longer.R +++ b/R/separate-longer.R @@ -87,6 +87,7 @@ map_unchop <- function(data, cols, fun, ..., .keep_empty = FALSE, .error_call = data = data, allow_rename = FALSE, allow_empty = FALSE, + error_arg = "cols", error_call = .error_call ) col_names <- names(cols) diff --git a/R/separate-rows.R b/R/separate-rows.R index ce9c2e07b..3e7370c76 100644 --- a/R/separate-rows.R +++ b/R/separate-rows.R @@ -44,7 +44,12 @@ separate_rows.data.frame <- function(data, check_string(sep) check_bool(convert) - vars <- tidyselect::eval_select(expr(c(...)), data, allow_rename = FALSE) + vars <- tidyselect::eval_select( + expr(c(...)), + data, + allow_rename = FALSE, + error_arg = "..." + ) vars <- names(vars) out <- purrr::modify_at(data, vars, str_split_n, pattern = sep) diff --git a/R/separate-wider.R b/R/separate-wider.R index a92074bbd..a0d5b415e 100644 --- a/R/separate-wider.R +++ b/R/separate-wider.R @@ -495,6 +495,7 @@ map_unpack <- function(data, cols, fun, names_sep, names_repair, error_call = ca data = data, allow_rename = FALSE, allow_empty = FALSE, + error_arg = "cols", error_call = error_call ) col_names <- names(cols) diff --git a/R/unite.R b/R/unite.R index 35eb4f58b..d88f46d7a 100644 --- a/R/unite.R +++ b/R/unite.R @@ -48,7 +48,7 @@ unite.data.frame <- function(data, col, ..., sep = "_", remove = TRUE, na.rm = F if (dots_n(...) == 0) { selection <- set_names(seq_along(data), names(data)) } else { - selection <- tidyselect::eval_select(expr(c(...)), data, allow_rename = FALSE) + selection <- tidyselect::eval_select(expr(c(...)), data, allow_rename = FALSE, error_arg = "...") } empty_selection <- length(selection) == 0L diff --git a/R/unnest-longer.R b/R/unnest-longer.R index acb21e93d..5e5059bfd 100644 --- a/R/unnest-longer.R +++ b/R/unnest-longer.R @@ -90,7 +90,7 @@ unnest_longer <- function(data, error_call <- current_env() - cols <- tidyselect::eval_select(enquo(col), data, allow_rename = FALSE) + cols <- tidyselect::eval_select(enquo(col), data, allow_rename = FALSE, error_arg = "col") col_names <- names(cols) n_col_names <- length(col_names) diff --git a/R/unnest-wider.R b/R/unnest-wider.R index 0f2be7c8e..55091824d 100644 --- a/R/unnest-wider.R +++ b/R/unnest-wider.R @@ -93,7 +93,7 @@ unnest_wider <- function(data, error_call <- current_env() - cols <- tidyselect::eval_select(enquo(col), data, allow_rename = FALSE) + cols <- tidyselect::eval_select(enquo(col), data, allow_rename = FALSE, error_arg = "col") col_names <- names(cols) for (i in seq_along(cols)) { diff --git a/R/unnest.R b/R/unnest.R index fef793042..5fc9aa961 100644 --- a/R/unnest.R +++ b/R/unnest.R @@ -181,7 +181,8 @@ unnest.data.frame <- function(data, cols <- tidyselect::eval_select( expr = enquo(cols), data = data, - allow_rename = FALSE + allow_rename = FALSE, + error_arg = "cols" ) cols <- unname(cols) From cc71f07d47bf913d54662b46f8ae847b3141871b Mon Sep 17 00:00:00 2001 From: olivroy Date: Wed, 23 Oct 2024 11:14:25 -0400 Subject: [PATCH 2/4] Update snapshots --- tests/testthat/_snaps/pack.md | 1 + tests/testthat/_snaps/pivot-long.md | 3 ++- tests/testthat/_snaps/pivot-wide.md | 9 +++++---- tests/testthat/_snaps/unnest.md | 1 + 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/testthat/_snaps/pack.md b/tests/testthat/_snaps/pack.md index 2f304f7fc..3d6b5302a 100644 --- a/tests/testthat/_snaps/pack.md +++ b/tests/testthat/_snaps/pack.md @@ -109,6 +109,7 @@ Condition Error in `unpack()`: ! Can't rename variables in this context. + i `cols` can't be renamed. # unpack() validates its inputs diff --git a/tests/testthat/_snaps/pivot-long.md b/tests/testthat/_snaps/pivot-long.md index 477545174..6c0b2815a 100644 --- a/tests/testthat/_snaps/pivot-long.md +++ b/tests/testthat/_snaps/pivot-long.md @@ -69,7 +69,7 @@ Code (expect_error(pivot_longer(iris, matches("foo")))) Output - + Error in `pivot_longer()`: ! `cols` must select at least one column. @@ -80,6 +80,7 @@ Condition Error in `pivot_longer()`: ! Can't rename variables in this context. + i `cols` can't be renamed. # `names_to` is validated diff --git a/tests/testthat/_snaps/pivot-wide.md b/tests/testthat/_snaps/pivot-wide.md index 889a3a45c..a18daffdf 100644 --- a/tests/testthat/_snaps/pivot-wide.md +++ b/tests/testthat/_snaps/pivot-wide.md @@ -53,9 +53,9 @@ (expect_error(pivot_wider(df, names_from = starts_with("foo"), values_from = val)) ) Output - + Error in `pivot_wider()`: - ! Must select at least one item. + ! `names_from` must select at least one column. # `values_from` must identify at least 1 column (#1240) @@ -63,9 +63,9 @@ (expect_error(pivot_wider(df, names_from = key, values_from = starts_with("foo"))) ) Output - + Error in `pivot_wider()`: - ! Must select at least one item. + ! `values_from` must select at least one column. # `values_fn` emits an informative error when it doesn't result in unique values (#1238) @@ -183,6 +183,7 @@ Condition Error in `pivot_wider()`: ! Can't rename variables in this context. + i `id_cols` can't be renamed. # `id_expand` is validated diff --git a/tests/testthat/_snaps/unnest.md b/tests/testthat/_snaps/unnest.md index d470b3867..9c985ac5f 100644 --- a/tests/testthat/_snaps/unnest.md +++ b/tests/testthat/_snaps/unnest.md @@ -65,6 +65,7 @@ Condition Error in `unnest()`: ! Can't rename variables in this context. + i `cols` can't be renamed. # cols must go in cols From 1c1db1897d44609fa477a92f82547b326959563c Mon Sep 17 00:00:00 2001 From: olivroy Date: Wed, 23 Oct 2024 11:14:39 -0400 Subject: [PATCH 3/4] Require dev tidyselect --- DESCRIPTION | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 85819cae7..78ef339b7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -30,9 +30,11 @@ Imports: rlang (>= 1.1.1), stringr (>= 1.5.0), tibble (>= 2.1.1), - tidyselect (>= 1.2.1), + tidyselect (>= 1.2.1.9000), utils, vctrs (>= 0.5.2) +Remotes: + r-lib/tidyselect#358 Suggests: covr, data.table, From 2c2a738a4dccef2c1e474cb9be7aecec596cd2f8 Mon Sep 17 00:00:00 2001 From: olivroy <52606734+olivroy@users.noreply.github.com> Date: Fri, 25 Oct 2024 08:29:26 -0400 Subject: [PATCH 4/4] main branch tidyselect --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 78ef339b7..4a3a498a1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -34,7 +34,7 @@ Imports: utils, vctrs (>= 0.5.2) Remotes: - r-lib/tidyselect#358 + r-lib/tidyselect Suggests: covr, data.table,