Skip to content

Commit

Permalink
Support S3 vectors in one_of()
Browse files Browse the repository at this point in the history
Part of r-lib#109
  • Loading branch information
lionel- committed Sep 11, 2019
1 parent cbb97bf commit 324b3bc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

# tidyselect 0.2.5.9000

* `vars_select()` now supports unquoting factors and other S3 vectors.
* `one_of()` now supports factors and other S3 vectors. Similary,
`vars_select()` now supports unquoting S3 vectors.

* `vars_rename()` no longer ignore the names of unquoted character
vectors of length 1 (#79).
Expand Down
8 changes: 5 additions & 3 deletions R/select-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ num_range <- function(prefix, range, width = NULL, vars = peek_vars()) {
one_of <- function(..., .vars = peek_vars()) {
keep <- list(...)

bad_input <- detect_index(keep, negate(is.character))
bad_input <- detect_index(keep, ~ !vec_is_coercible(., chr()))
if (bad_input) {
type <- friendly_type_of(keep)
abort(glue::glue("Input { bad_input } must be a character vector, not { type }"))
type <- friendly_type_of(keep[[bad_input]])
msg <- glue::glue("Input { bad_input } must be a vector of column names, not {type}.")
abort(msg, "tidyselect_error_incompatible_index_type")
}

keep <- vctrs::vec_c(!!!keep, .ptype = character())

if (!all(keep %in% .vars)) {
Expand Down
9 changes: 7 additions & 2 deletions tests/testthat/test-select-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ test_that("order is determined from inputs (#53)", {
test_that("one_of gives useful errors", {
expect_error(
one_of(1L, .vars = c("x", "y")),
"Input 1 must be a character vector, not a list",
fixed = TRUE
"Input 1 must be a vector of column names, not an integer vector",
fixed = TRUE,
class = "tidyselect_error_incompatible_index_type"
)
})

Expand Down Expand Up @@ -273,3 +274,7 @@ test_that("last_col() selects last argument with offset", {
expect_error(last_col(3, vars), "`offset` must be smaller than the number of columns")
expect_error(last_col(vars = chr()), "Can't select last column when input is empty")
})

test_that("one_of() supports S3 vectors", {
expect_identical(vars_select(letters, one_of(factor(c("a", "c")))), c(a = "a", c = "c"))
})

0 comments on commit 324b3bc

Please sign in to comment.