Skip to content

Commit

Permalink
prefer vctrs::new_data_frame() over tibble(!!!) (#5371)
Browse files Browse the repository at this point in the history
* prefer vctrs::new_data_frame()

* Let vctrs::rbind() handle vctrs, but keep dealing with lists for now

* using expect_equivalent()

* make it clearer that bare lists are the special case

* make test explicit
  • Loading branch information
romainfrancois committed Jul 3, 2020
1 parent dd91f02 commit 4d93ff0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 7 additions & 1 deletion R/bind.r
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ bind_rows <- function(..., .id = NULL) {
}
}

dots <- map(dots, function(.x) if (is.data.frame(.x)) .x else tibble(!!!.x))
dots <- map(dots, function(.x) {
if (vec_is_list(.x)) {
.x <- new_data_frame(as.list(.x))
}
.x
})

if (is.null(.id)) {
names(dots) <- NULL
}
Expand Down
11 changes: 6 additions & 5 deletions tests/testthat/test-bind.R
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,8 @@ test_that("bind_rows() handles rowwise vectors", {

test_that("bind_rows() accepts lists of dataframe-like lists as first argument", {
ll <- list(a = 1, b = 2)
df <- tibble(a = 1, b = 2)

expect_equal(bind_rows(list(ll)), df)
expect_equal(bind_rows(list(ll, ll)), df[c(1, 1), ])
expect_equal(bind_rows(list(ll)), data.frame(a = 1, b = 2))
expect_equal(bind_rows(list(ll, ll)), data.frame(a = c(1, 1), b = c(2, 2)))
})

test_that("bind_rows can handle lists (#1104)", {
Expand Down Expand Up @@ -523,7 +521,10 @@ test_that("bind_rows() handles named S3 objects (#4931)", {

expect_identical(
bind_rows(fct, fct),
tibble(x = unname(fct)[c(1, 1)], y = unname(fct)[c(2, 2)])
data.frame(
x = factor(c("a", "a"), levels = c("a", "b")),
y = factor(c("b", "b"), levels = c("a", "b"))
)
)
})

Expand Down

0 comments on commit 4d93ff0

Please sign in to comment.