Skip to content

Commit

Permalink
tolerate unknown vars in one_of() (tidyverse#1848)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jennifer (Jenny) Bryan authored and hadley committed May 26, 2016
1 parent b42f20a commit f796070
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

* enabling joining of data frames that don't have the same encoding of
column names (#1513).

* `one_of()` tolerates unknown variables in `vars`, but warns (#1848, @jennybc).

## Breaking changes

Expand Down
4 changes: 2 additions & 2 deletions R/select-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' \item \code{contains()}: contains a literal string
#' \item \code{matches()}: matches a regular expression
#' \item \code{num_range()}: a numerical range like x01, x02, x03.
#' \item \code{one_of()}: varables in character vector.
#' \item \code{one_of()}: variables in character vector.
#' \item \code{everything()}: all variables.
#' }
#'
Expand Down Expand Up @@ -116,7 +116,7 @@ one_of <- function(..., vars = current_vars()) {

if (!all(keep %in% vars)) {
bad <- setdiff(keep, vars)
stop("Unknown variables: ", paste0("`", bad, "`", collapse = ", "))
warning("Unknown variables: ", paste0("`", bad, "`", collapse = ", "))
}

match_vars(keep, vars)
Expand Down
2 changes: 1 addition & 1 deletion man/select_helpers.Rd

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

11 changes: 8 additions & 3 deletions tests/testthat/test-select-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,17 @@ test_that("num_range selects numeric ranges", {
# one_of ------------------------------------------------------------------

test_that("one_of gives useful errors", {
expect_error(one_of(1L, vars = c("x", "y")), "must be a character vector")
})

test_that("one_of tolerates but warns for unknown variables", {
vars <- c("x", "y")

expect_error(one_of("z", vars = vars), "Unknown variables: `z`")
expect_error(one_of(c("x", "z"), vars = vars), "Unknown variables: `z`")
expect_warning(res <- one_of("z", vars = vars), "Unknown variables: `z`")
expect_equal(res, -(1:2))
expect_warning(res <- one_of(c("x", "z"), vars = vars), "Unknown variables: `z`")
expect_equal(res, 1L)

expect_error(one_of(1L, vars = vars), "must be a character vector")
})

test_that("one_of converts names to positions", {
Expand Down

0 comments on commit f796070

Please sign in to comment.