Skip to content

Commit

Permalink
Fix colwise issue when predicate returns a FALSE vector
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Jul 11, 2016
1 parent 8b28b0b commit 272a26a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
@@ -1,5 +1,8 @@
# dplyr 0.5.0.9000

* Fix issue with `mutate_if()` and `summarise_if()` when a predicate
function returns a vector of `FALSE` (#1989, #2009).

# dplyr 0.5.0

## Breaking changes
Expand Down
15 changes: 10 additions & 5 deletions R/colwise.R
Expand Up @@ -70,15 +70,17 @@
#' @export
summarise_all <- function(.tbl, .funs, ...) {
funs <- as.fun_list(.funs, .env = parent.frame(), ...)
vars <- colwise_(.tbl, funs, list())
cols <- lazyeval::lazy_dots(everything())
vars <- colwise_(.tbl, funs, cols)
summarise_(.tbl, .dots = vars)
}

#' @rdname summarise_all
#' @export
mutate_all <- function(.tbl, .funs, ...) {
funs <- as.fun_list(.funs, .env = parent.frame(), ...)
vars <- colwise_(.tbl, funs, list())
cols <- lazyeval::lazy_dots(everything())
vars <- colwise_(.tbl, funs, cols)
mutate_(.tbl, .dots = vars)
}

Expand Down Expand Up @@ -192,9 +194,6 @@ colwise_ <- function(tbl, calls, vars) {
named_calls <- attr(calls, "has_names")
named_vars <- any(has_names(vars))

if (length(vars) == 0) {
vars <- lazyeval::lazy_dots(everything())
}
vars <- select_vars_(tbl_vars(tbl), vars, exclude = as.character(groups(tbl)))

out <- vector("list", length(vars) * length(calls))
Expand Down Expand Up @@ -248,6 +247,9 @@ summarise_each <- function(tbl, funs, ...) {
#' @export
#' @rdname summarise_each
summarise_each_ <- function(tbl, funs, vars) {
if (length(vars) == 0) {
vars <- lazyeval::lazy_dots(everything())
}
if (is.character(funs)) {
funs <- funs_(funs)
}
Expand Down Expand Up @@ -277,6 +279,9 @@ mutate_each <- function(tbl, funs, ...) {
#' @export
#' @rdname summarise_each
mutate_each_ <- function(tbl, funs, vars) {
if (length(vars) == 0) {
vars <- lazyeval::lazy_dots(everything())
}
vars <- colwise_(tbl, funs, vars)
mutate_(tbl, .dots = vars)
}
Expand Down
10 changes: 9 additions & 1 deletion tests/testthat/test-colwise.R
Expand Up @@ -74,13 +74,21 @@ test_that("sql sources fail with bare functions", {
expect_error(memdb_frame(x = 1) %>% mutate_all(mean) %>% collect())
})

test_that("empty selection does not select everything (#2009, #1989)", {
expect_equal(mtcars, mutate_if(mtcars, is.factor, as.character))
})


# Deprecated ---------------------------------------------------------

test_that("summarise_each() and summarise_all() agree", {
test_that("_each() and _all() families agree", {
df <- data.frame(x = 1:3, y = 1:3)

expect_equal(summarise_each(df, funs(mean)), summarise_all(df, mean))
expect_equal(summarise_each(df, funs(mean), x:y), summarise_at(df, vars(x:y), mean))
expect_equal(summarise_each(df, funs(mean), z = y), summarise_at(df, vars(z = y), mean))

expect_equal(mutate_each(df, funs(mean)), mutate_all(df, mean))
expect_equal(mutate_each(df, funs(mean), x:y), mutate_at(df, vars(x:y), mean))
expect_equal(mutate_each(df, funs(mean), z = y), mutate_at(df, vars(z = y), mean))
})

0 comments on commit 272a26a

Please sign in to comment.