Skip to content

Commit

Permalink
Revert "Fix mapping to data frames with functions returning vectors" (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored Oct 17, 2017
1 parent d15370f commit 1fcba93
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 26 deletions.
3 changes: 0 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@

# purrr 0.2.3.9000

* `map_dfr()` and `map_dfc()` now handle functions returning vectors
correctly (#179).


# purrr 0.2.3

Expand Down
4 changes: 2 additions & 2 deletions R/map.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ map_dfr <- function(.x, .f, ..., .id = NULL) {

.f <- as_mapper(.f, ...)
res <- map(.x, .f, ...)
dplyr::bind_rows(!!! res, .id = .id)
dplyr::bind_rows(res, .id = .id)
}

#' @rdname map
Expand All @@ -183,7 +183,7 @@ map_dfc <- function(.x, .f, ...) {

.f <- as_mapper(.f, ...)
res <- map(.x, .f, ...)
dplyr::bind_cols(!!! res)
dplyr::bind_cols(res)
}

#' @export
Expand Down
30 changes: 9 additions & 21 deletions tests/testthat/test-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ test_that("map forces arguments in same way as base R", {
expect_equal(f_map[[2]](0), f_base[[2]](0))
})

test_that("row and column binding work", {
mtcar_mod <- mtcars %>%
split(.$cyl) %>%
map(~ lm(mpg ~ wt, data = .x))
f_coef <- function(x) as.data.frame(t(as.matrix(coef(x))))
expect_length(mtcar_mod %>% map_dfr(f_coef), 2)
expect_length(mtcar_mod %>% map_dfc(f_coef), 6)
})

test_that("walk is used for side-effects", {
expect_output(walk(1:3, str))
})
Expand All @@ -62,24 +71,3 @@ test_that("map_if() and map_at() always return a list", {
expect_identical(map_if(df, is.character, ~"out"), list(x = 1, y = "out"))
expect_identical(map_at(df, 1, ~"out"), list(x = "out", y = "a"))
})

test_that("map_dfr() and map_dfc() handle data frames", {
tbl <- tibble::tibble(x = 1, y = 2)
expect_identical(map_dfr(1:2, ~tbl), dplyr::bind_rows(tbl, tbl))
expect_identical(map_dfc(1:2, ~tbl), dplyr::bind_cols(tbl, tbl))
})

test_that("map_dfr() and map_dfc() handle dataframeable lists", {
tbl <- tibble::tibble(x = 1, y = 2)
list <- list(x = 1, y = 2)
expect_identical(map_dfr(1:2, ~list), dplyr::bind_rows(tbl, tbl))
expect_identical(map_dfc(1:2, ~list), dplyr::bind_cols(tbl, tbl))
})

test_that("map_dfr() and map_dfc() handle named vectors", {
row <- c(x = 1, y = 2)
expect_identical(map_dfr(1:2, ~row), tibble::tibble(x = c(1, 1), y = c(2, 2)))

col <- c(1, 2)
expect_identical(map_dfc(c(w = 0, z = 0), ~col), tibble::tibble(w = col, z = col))
})

0 comments on commit 1fcba93

Please sign in to comment.