Skip to content

Commit

Permalink
Fix edge case when coercing data frames to matrices (#7070)
Browse files Browse the repository at this point in the history
Closes #7004
  • Loading branch information
krlmlr authored Aug 16, 2024
1 parent b2c7e04 commit 173b423
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# dplyr (development version)

* Fixed an edge case when coercing data frames to matrices (#7004).

* Fixed an issue where duckplyr's ALTREP data frames were being materialized
early due to internal usage of `ncol()` (#7049).

Expand Down
17 changes: 16 additions & 1 deletion R/data-mask.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@ DataMask <- R6Class("DataMask",
frame <- caller_env(n = 2)
local_mask(self, frame)

names_bindings <- chr_unserialise_unicode(names2(data))
names <- names(data)

if (is.null(names)) {
cli::cli_abort(
"Can't transform a data frame with `NULL` names.",
call = error_call
)
}
if (vec_any_missing(names)) {
cli::cli_abort(
"Can't transform a data frame with missing names.",
call = error_call
)
}

names_bindings <- chr_unserialise_unicode(names)
if (any(names_bindings == "")) {
# `names2()` converted potential `NA` names to `""` already
abort("Can't transform a data frame with `NA` or `\"\"` names.", call = error_call)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
filter(df2)
Condition
Error in `filter()`:
! Can't transform a data frame with `NA` or `""` names.
! Can't transform a data frame with missing names.

# can't use `.by` with `.preserve`

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/mutate.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,5 +361,5 @@
mutate(df2)
Condition
Error in `mutate()`:
! Can't transform a data frame with `NA` or `""` names.
! Can't transform a data frame with missing names.

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/summarise.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
summarise(df2)
Condition
Error in `summarise()`:
! Can't transform a data frame with `NA` or `""` names.
! Can't transform a data frame with missing names.

# summarise() gives meaningful errors

Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-data-mask.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test_that("Empty matrix can be coerced to a data frame (#7004)", {
skip_if_not(getRversion() >= "4.4")
expect_equal(
slice(as.data.frame(matrix(nrow = 0, ncol = 0)), 1),
data.frame()
)
})

0 comments on commit 173b423

Please sign in to comment.