Skip to content

Commit 126736f

Browse files
authored
Correctly allocate logical of length 0 in rowwise edge case (#7749)
1 parent 957ee28 commit 126736f

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

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+
* Empty `rowwise()` list-column elements now resolve to `logical()` rather than a random logical of length 1 (#7710).
4+
35
* `last_dplyr_warnings()` no longer prevents objects from being garbage collected (#7649).
46

57
* Progress towards making dplyr conformant with the public C API of R (#7741).

src/chop.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void dplyr_lazy_vec_chop_grouped(SEXP chops_env, SEXP rows, SEXP data, bool roww
2626
SET_VECTOR_ELT(column, 0, ptype);
2727
} else {
2828
// i.e. `vec_ptype_finalise(unspecified())` (#6369)
29-
SET_VECTOR_ELT(column, 0, Rf_allocVector(LGLSXP, 1));
29+
SET_VECTOR_ELT(column, 0, Rf_allocVector(LGLSXP, 0));
3030
}
3131
SET_PRCODE(prom, column);
3232
UNPROTECT(2);

tests/testthat/test-mutate.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,14 @@ test_that("mutate preserves class of zero-row rowwise (#4224, #6303)", {
332332
out <- mutate(rf, x2 = identity(x), x3 = x)
333333
expect_equal(out$x2, logical())
334334
expect_equal(out$x3, logical())
335+
336+
# with the empty list case, `x` is `logical()`, not a random logical of length
337+
# 1 that happens to get recycled to length 0 (#7710)
338+
rf <- rowwise(tibble(x = list()))
339+
out <- mutate(rf, x2 = {
340+
expect_identical(x, logical())
341+
x
342+
})
335343
})
336344

337345
test_that("mutate works on empty data frames (#1142)", {

0 commit comments

Comments
 (0)