Skip to content

Commit ac76744

Browse files
committed
Add rbind.rowwise_df() method
1 parent 2f9a846 commit ac76744

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ S3method(print,last_dplyr_warnings)
117117
S3method(print,src)
118118
S3method(pull,data.frame)
119119
S3method(rbind,grouped_df)
120+
S3method(rbind,rowwise_df)
120121
S3method(recode,character)
121122
S3method(recode,factor)
122123
S3method(recode,numeric)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# dplyr (development version)
22

3+
* New `rbind()` method for `rowwise_df` to avoid creating corrupt rowwise data frames (r-lib/vctrs#1935).
4+
35
* In `case_when()`, supplying all size 1 LHS inputs along with a size >1 RHS input is now soft-deprecated. This is an improper usage of `case_when()` that should instead be a series of if statements, like:
46

57
```

R/rowwise.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,8 @@ as_tibble.rowwise_df <- function(x, ...) {
171171

172172
rowwise_df(data, group_vars)
173173
}
174+
175+
#' @export
176+
rbind.rowwise_df <- function(...) {
177+
bind_rows(...)
178+
}

tests/testthat/test-rowwise.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,33 @@ test_that("new_rowwise_df() can add class and attributes (#5918)", {
8989
expect_equal(attr(df, "a"), "b")
9090
})
9191

92+
test_that("rbind() works with rowwise data frames by calling bind_rows() (r-lib/vctrs#1935)", {
93+
x <- rowwise(tibble(a = 1:2))
94+
95+
y <- rowwise(tibble(a = 3:4))
96+
out <- rbind(x, y)
97+
expect_identical(out, rowwise(tibble(a = c(1:2, 3:4))))
98+
99+
# Important that `.rows` is recreated, not copied over from `x` (r-lib/vctrs#1935)
100+
expect_identical(
101+
group_data(out),
102+
new_tibble(list(.rows = list_of(1L, 2L, 3L, 4L)))
103+
)
104+
105+
# `bind_rows()` returns an object with the class of the first input,
106+
# which is roughly how `rbind()` also works
107+
108+
# With bare tibble
109+
y <- tibble(a = 5:6)
110+
out <- rbind(x, z)
111+
expect_identical(out, rowwise(tibble(a = c(1:2, 5:6))))
112+
113+
# With grouped_df
114+
y <- group_by(tibble(a = 5:6), a)
115+
out <- rbind(x, z)
116+
expect_identical(out, rowwise(tibble(a = c(1:2, 5:6))))
117+
})
118+
92119
test_that("validate_rowwise_df() gives useful errors", {
93120
df1 <- rowwise(tibble(x = 1:4, g = rep(1:2, each = 2)), g)
94121
groups <- attr(df1, "groups")

0 commit comments

Comments
 (0)