Skip to content

Commit

Permalink
Add suffix argument to num_range() (#286)
Browse files Browse the repository at this point in the history
Fixes #229
  • Loading branch information
hadley committed Aug 15, 2022
1 parent 9550f03 commit b8bd945
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# tidyselect (development version)

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

* `eval_select()` always returns a named vector, even when renaming is not
permitted (#220).

Expand Down
5 changes: 3 additions & 2 deletions R/helpers-pattern.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,14 @@ matches <- function(match,
}

#' @rdname starts_with
#' @param prefix A prefix that starts the numeric range.
#' @param prefix,suffix A prefix/suffix added before/after the numeric range.
#' @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.
#' @export
num_range <- function(prefix,
range,
suffix = "",
width = NULL,
vars = NULL) {
vars <- vars %||% peek_vars(fn = "num_range")
Expand All @@ -183,7 +184,7 @@ num_range <- function(prefix,
range <- sprintf(paste0("%0", width, "d"), range)
}

match_vars(paste0(prefix, range), vars)
match_vars(paste0(prefix, range, suffix), vars)
}

check_match <- function(match) {
Expand Down
4 changes: 2 additions & 2 deletions man/starts_with.Rd

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

5 changes: 5 additions & 0 deletions tests/testthat/test-helpers-pattern.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ test_that("num_range selects numeric ranges", {
expect_equal(select_loc(vars, num_range("x", 10:11, width = 2)), c(x10 = 5L, x11 = 6L))
})

test_that("num_range can use a suffix (#229)", {
vars <- set_names(c("x1", "x2", "x1_y", "x2_y"))
expect_named(select_loc(vars, num_range("x", 1:2, "_y")), c("x1_y", "x2_y"))
})


test_that("matchers accept length > 1 vectors (#50)", {
expect_identical(
Expand Down

0 comments on commit b8bd945

Please sign in to comment.