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 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
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
18 changes: 18 additions & 0 deletions R/helpers-pattern.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,32 @@
#' @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][vctrs::vector_recycling_rules].
#' @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()
check_bool(cross)
check_number_whole(width, allow_null = TRUE)

vars <- vars %||% peek_vars(fn = "num_range")

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

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

View check run for this annotation

Codecov / codecov/patch

R/helpers-pattern.R#L195

Added line #L195 was not covered by tests
} 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
Loading
Loading