Skip to content

Commit

Permalink
Fix edge case when coercing data frames to matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Aug 15, 2024
1 parent cdc9919 commit 08f7436
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 10 additions & 1 deletion R/data-mask.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ 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)) {
abort("Can't transform a data frame with `NULL` names.")
}
if (vec_any_missing(names)) {
abort("Can't transform a data frame with missing names.")
}

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
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_error(
slice(as.data.frame(matrix(nrow = 0, ncol = 0)), 1),
NA
)
})

0 comments on commit 08f7436

Please sign in to comment.