From 272a26a0bd62a74c51a2702a94d403a9762108b8 Mon Sep 17 00:00:00 2001 From: Lionel Henry Date: Mon, 11 Jul 2016 11:28:21 +0200 Subject: [PATCH] Fix colwise issue when predicate returns a FALSE vector Closes #1989 --- NEWS.md | 3 +++ R/colwise.R | 15 ++++++++++----- tests/testthat/test-colwise.R | 10 +++++++++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/NEWS.md b/NEWS.md index 8fb20c5a95..68ab98b3a8 100644 --- a/NEWS.md +++ b/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 diff --git a/R/colwise.R b/R/colwise.R index 6049448221..118dc39b63 100644 --- a/R/colwise.R +++ b/R/colwise.R @@ -70,7 +70,8 @@ #' @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) } @@ -78,7 +79,8 @@ summarise_all <- function(.tbl, .funs, ...) { #' @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) } @@ -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)) @@ -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) } @@ -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) } diff --git a/tests/testthat/test-colwise.R b/tests/testthat/test-colwise.R index 581c7413ba..bcbeba5903 100644 --- a/tests/testthat/test-colwise.R +++ b/tests/testthat/test-colwise.R @@ -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)) })