Skip to content

Commit f30b5d9

Browse files
committed
Need to preserve dim and dimnames
1 parent 570fefa commit f30b5d9

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

R/subsetting-matrix.R

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ tbl_subassign_matrix <- function(x, j, value, j_arg, value_arg) {
2828

2929
withCallingHandlers(
3030
for (j in col_idx) {
31-
x[[j]] <- vectbl_assign_matrix(x[[j]], cells[[j]], value)
31+
x[[j]] <- vectbl_assign(x[[j]], cells[[j]], value)
3232
},
3333

3434
vctrs_error_incompatible_type = function(cnd) {
@@ -61,15 +61,6 @@ cells_to_col_idx <- function(cells) {
6161
col_idx
6262
}
6363

64-
vectbl_assign_matrix <- function(x, i, value) {
65-
d <- dim(x)
66-
dn <- dimnames(x)
67-
x <- vectbl_assign(x, i, value)
68-
dim(x) <- d
69-
dimnames(x) <- dn
70-
x
71-
}
72-
7364
# Errors ------------------------------------------------------------------
7465

7566
error_subset_matrix_must_be_logical <- function(j_arg) {

R/subsetting.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,11 @@ vectbl_assign <- function(x, i, value) {
668668
}
669669
} else {
670670
if (.Call("tibble_need_coerce", x)) {
671+
d <- dim(x)
672+
dn <- dimnames(x)
671673
x <- vec_slice(value, rep(NA_integer_, length(x)))
674+
dim(x) <- d
675+
dimnames(x) <- dn
672676
}
673677
}
674678

tests/testthat/_snaps/subsetting.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -557,16 +557,18 @@
557557
`NULL` must be a vector, a bare list, a data frame or a matrix.
558558
Code
559559
# # [<-.tbl_df and overwriting NA
560-
df <- tibble(x = rep(NA, 3))
560+
df <- tibble(x = rep(NA, 3), z = matrix(NA, ncol = 2, dimnames = list(NULL, c(
561+
"a", "b"))))
561562
df[1, "x"] <- 5
563+
df[1, "z"] <- 5
562564
df
563565
Output
564-
# A tibble: 3 x 1
565-
x
566-
<dbl>
567-
1 5
568-
2 NA
569-
3 NA
566+
# A tibble: 3 x 2
567+
x z[,"a"] [,"b"]
568+
<dbl> <dbl> <dbl>
569+
1 5 5 5
570+
2 NA NA NA
571+
3 NA NA NA
570572
Code
571573
# # [<-.tbl_df and overwriting with NA
572574
df <- tibble(a = TRUE, b = 1L, c = sqrt(2), d = 0+3i + 1, e = "e", f = raw(1),

tests/testthat/test-subsetting.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,9 @@ test_that("output test", {
924924
df[1:3, 1:3] <- NULL
925925

926926
"# [<-.tbl_df and overwriting NA"
927-
df <- tibble(x = rep(NA, 3))
927+
df <- tibble(x = rep(NA, 3), z = matrix(NA, ncol = 2, dimnames = list(NULL, c("a", "b"))))
928928
df[1, "x"] <- 5
929+
df[1, "z"] <- 5
929930
df
930931

931932
"# [<-.tbl_df and overwriting with NA"

0 commit comments

Comments
 (0)