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

Add cross argument to num_range() #362

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 18 additions & 14 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# tidyselect (development version)

* `num_range()` now recycles its arguments using tidyverse rules (#355).
In addition, it gains a `cross` argument that allows you to take the
cartesian product of these arguments instead.

* `eval_select(allow_empty = FALSE)` gains a new argument to yield a better error
message in case of empty selection (@olivroy, #327)

* `eval_select()` and `eval_relocate()` gain a new `error_arg` argument that can be specified to throw a better error message when `allow_empty = FALSE`.

* `eval_select()` and `eval_relocate()` throw a classed error message when `allow_empty = FALSE` (@olivroy, #347).
Expand Down Expand Up @@ -45,17 +49,17 @@

* `any_of()` generates a more informative error if you supply too many
arguments (#241).
* `all_of()` (like `any_of()`) returns an integer vector to make it easier
to combine in functions (#270, #294). It also fails when it can't find

* `all_of()` (like `any_of()`) returns an integer vector to make it easier
to combine in functions (#270, #294). It also fails when it can't find
variables even when `strict = FALSE`.

* `matches()` recognises and correctly uses stringr pattern objects
(`stringr::regex()`, `stringr::fixed()`, etc) (#238). It also now
works with named vectors (#250).

* `num_range()` gains a `suffix` argument (#229).

* `where()` is now exported, like all other select helpers (#201),
and gives more informative errors (#236).

Expand All @@ -65,27 +69,27 @@
* `eval_select()` always returns a named vector, even when renaming is not
permitted (#220).

* `eval_select()` and `eval_relocate()` gain new `allow_empty` argument which
* `eval_select()` and `eval_relocate()` gain new `allow_empty` argument which
makes it possible to forbid empty selections with `allow_empty = FALSE` (#252).

* `eval_select(allow_rename = FALSE)` no longer fails with empty
selections (#221, @eutwt) or with predicate functions (#225). It now properly
selections (#221, @eutwt) or with predicate functions (#225). It now properly
fails with partial renaming (#305).

* `peek_var()` error now generates hyperlink to docs with recent RStudio (#289).

* `vars_pull()` generates more informative error messages (#234, #258, #318)
and gains `error_call` and `error_arg` arguments.

* Errors produced by tidyselect should now be more informative. Evaluation
errors are now chained, with the child error call is set to the `error_call`
argument of `eval_select()` and `eval_rename()`. We've also improved
backtraces of base errors, and done better at propagating the root
* Errors produced by tidyselect should now be more informative. Evaluation
errors are now chained, with the child error call is set to the `error_call`
argument of `eval_select()` and `eval_rename()`. We've also improved
backtraces of base errors, and done better at propagating the root
`error_call` to vctrs input checkers.

* `tidyselect_verbosity` is no longer used; deprecation messaging is now
controlled by `lifecycle_verbosity` like all other packages (#317).

# tidyselect 1.1.2

* Fix for CRAN checks.
Expand Down
15 changes: 15 additions & 0 deletions R/helpers-pattern.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,29 @@
#' @param range A sequence of integers, like `1:5`.
#' @param width Optionally, the "width" of the numeric range. For example,
#' a range of 2 gives "01", a range of three "001", etc.
#' @inheritParams rlang::args_dots_empty
#' @param cross Whether to take the cartesian product of `prefix`, `range`, and `suffix`.
#' If `FALSE`, the default, these arguments are recycled using tidyverse rules.
lionel- marked this conversation as resolved.
Show resolved Hide resolved
#' @export
num_range <- function(prefix,
range,
suffix = "",
width = NULL,
...,
cross = FALSE,
lionel- marked this conversation as resolved.
Show resolved Hide resolved
vars = NULL) {
check_dots_empty()
vars <- vars %||% peek_vars(fn = "num_range")

if (cross) {
args <- vctrs::vec_expand_grid(prefix = prefix, range = range, suffix = suffix)

Check warning on line 192 in R/helpers-pattern.R

View check run for this annotation

Codecov / codecov/patch

R/helpers-pattern.R#L192

Added line #L192 was not covered by tests
lionel- marked this conversation as resolved.
Show resolved Hide resolved
} else {
args <- vctrs::vec_recycle_common(prefix = prefix, range = range, suffix = suffix)
}
prefix <- args$prefix
range <- args$range
suffix <- args$suffix

if (!is_null(width)) {
lionel- marked this conversation as resolved.
Show resolved Hide resolved
range <- sprintf(paste0("%0", width, "d"), range)
}
Expand Down
2 changes: 1 addition & 1 deletion man/faq-selection-context.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion man/starts_with.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions tests/testthat/_snaps/helpers-pattern.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# num_range recycles with tidyverse rules (#355)

Code
select_loc(vars, num_range(c("x", "y"), 1:3))
Condition
Error in `select_loc()`:
i In argument: `num_range(c("x", "y"), 1:3)`.
Caused by error in `num_range()`:
! Can't recycle `prefix` (size 2) to match `range` (size 3).

---

Code
select_loc(vars, num_range(c("x", "y"), 1:2, c("_foo", "_bar", "_baz")))
Condition
Error in `select_loc()`:
i In argument: `num_range(c("x", "y"), 1:2, c("_foo", "_bar", "_baz"))`.
Caused by error in `num_range()`:
! Can't recycle `prefix` (size 2) to match `suffix` (size 3).

# matches() complains about bad stringr pattern usage

Code
Expand Down
43 changes: 43 additions & 0 deletions tests/testthat/test-helpers-pattern.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,49 @@ test_that("num_range can use a suffix (#229)", {
expect_named(select_loc(vars, num_range("x", 1:2, "_y")), c("x1_y", "x2_y"))
})

test_that("num_range recycles with tidyverse rules (#355)", {
vars <- set_names(c("x1", "y1", "y2", "x2", "x3"))
expect_snapshot(
error = TRUE,
select_loc(vars, num_range(c("x", "y"), 1:3)),
)
expect_named(
select_loc(vars, num_range(c("x", "y"), 1:2)),
c("x1", "y2")
)

vars <- set_names(c("x1_foo", "y1_bar", "y1_foo", "x2_bar", "x3_bar", "y2_bar"))
expect_named(
select_loc(vars, num_range(c("x", "y"), 1:2, "_foo")),
"x1_foo"
)
expect_named(
select_loc(vars, num_range(c("x", "y"), 1:2, c("_foo", "_bar"))),
c("x1_foo", "y2_bar")
)
expect_snapshot(
error = TRUE,
select_loc(vars, num_range(c("x", "y"), 1:2, c("_foo", "_bar", "_baz"))),
)
})

test_that("num_range crosses ranges with prefixes and suffixes if requested (#355)", {
vars <- set_names(c("x1", "y1", "y2", "x2", "x3"))
expect_named(
select_loc(vars, num_range(c("x", "y"), 1:3, cross = TRUE)),
c("x1", "x2", "x3", "y1", "y2")
)

vars <- set_names(c("x1_foo", "y1_bar", "y1_foo", "x2_bar", "x3_bar", "y2_bar"))
expect_named(
select_loc(vars, num_range(c("x", "y"), 1:3, "_foo", cross = TRUE)),
c("x1_foo", "y1_foo")
)
expect_named(
select_loc(vars, num_range(c("x", "y"), 1:3, c("_foo", "_bar"), cross = TRUE)),
c("x1_foo", "x2_bar", "x3_bar", "y1_foo", "y1_bar", "y2_bar")
)
})

test_that("matchers accept length > 1 vectors (#50)", {
expect_identical(
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-helpers.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

test_that("one_of gives useful errors", {
expect_snapshot(error = TRUE, cnd_class = TRUE, {
one_of(1L, .vars = c("x", "y"))
Expand Down
Loading