Skip to content

Commit

Permalink
select_if() and rename_if() handle lgl vectors.
Browse files Browse the repository at this point in the history
closes #4213
  • Loading branch information
romainfrancois committed Mar 5, 2019
1 parent 0ef611b commit 1a25d19
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

* colwise functions `summarise_at()` ... can rename vars in the case of multiple functions (#4180).

* `select_if()` and `rename_if()` handle logical vector predicate (#4213).

# dplyr 0.8.0.1 (2019-02-15)

* Fixed integer C/C++ division, forced released by CRAN (#4185).
Expand Down
8 changes: 6 additions & 2 deletions R/colwise-select.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ rename_all <- function(.tbl, .funs = list(), ...) {
#' @export
select_if <- function(.tbl, .predicate, .funs = list(), ...) {
funs <- as_fun_list(.funs, enquo(.funs), caller_env(), ...)
.predicate <- as_fun_list(.predicate, enquo(.predicate), caller_env())
if (!is_logical(.predicate)) {
.predicate <- as_fun_list(.predicate, enquo(.predicate), caller_env())
}
vars <- tbl_if_vars(.tbl, .predicate, caller_env(), .include_group_vars = TRUE)
syms <- vars_select_syms(vars, funs, .tbl)
select(.tbl, !!!syms)
Expand All @@ -73,7 +75,9 @@ select_if <- function(.tbl, .predicate, .funs = list(), ...) {
#' @export
rename_if <- function(.tbl, .predicate, .funs = list(), ...) {
funs <- as_fun_list(.funs, enquo(.funs), caller_env(), ...)
.predicate <- as_fun_list(.predicate, enquo(.predicate), caller_env())
if (!is_logical(.predicate)) {
.predicate <- as_fun_list(.predicate, enquo(.predicate), caller_env())
}
vars <- tbl_if_vars(.tbl, .predicate, caller_env(), .include_group_vars = TRUE)
syms <- vars_select_syms(vars, funs, .tbl, strict = TRUE)
rename(.tbl, !!!syms)
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-colwise-select.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,18 @@ test_that("mutate_all does not change the order of columns (#3351)", {
tbl <- group_by(tibble(x = 1:4, y = 1:4, z = 1:4), y)
expect_message(expect_identical(names(mutate_all(tbl, identity)), names(tbl)), "ignored")
})

test_that("select_if() and rename_if() handles logical (#4213)", {
ids <- "Sepal.Length"
expect_identical(
iris %>% select_if(!names(.) %in% ids),
iris %>% select(-Sepal.Length)
)

expect_identical(
iris %>% rename_if(!names(.) %in% ids, toupper),
iris %>% rename_at(setdiff(names(.), "Sepal.Length"), toupper)
)

})

0 comments on commit 1a25d19

Please sign in to comment.