Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix edge case when coercing data frames to matrices #7070

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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
)

Check warning on line 20 in R/data-mask.R

View check run for this annotation

Codecov / codecov/patch

R/data-mask.R#L17-L20

Added lines #L17 - L20 were not covered by tests
}
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()
)
})
Loading