From b3436df8325c2fdf7d89887f3d1d36000888deba Mon Sep 17 00:00:00 2001 From: Elin Waring Date: Sun, 5 Jul 2026 23:31:48 -0400 Subject: [PATCH 1/3] Assign dimnames in as.table. --- R/data_tabulate.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/R/data_tabulate.R b/R/data_tabulate.R index b659f3584..7eeb83122 100644 --- a/R/data_tabulate.R +++ b/R/data_tabulate.R @@ -757,6 +757,7 @@ as.table.datawizard_crosstab <- function( if (.is_grouped_df_xtab(x)) { x$Group <- NULL } + # first column contains the row names row_names <- as.character(x[[1]]) row_names[is.na(row_names)] <- "NA" @@ -777,6 +778,10 @@ as.table.datawizard_crosstab <- function( } # coerce to table result <- as.table(as.matrix(x)) + + dimnames_names <- c(attr(x, "varname"), attr(x, "by")) + names(attributes(result)$dimnames) <- dimnames_names + # if we don't want to simplify the table, we wrap it into a list if (!simplify) { result <- list(result) From 1a7ee3728d8ed81ebb34f5a6d2a257732804fa04 Mon Sep 17 00:00:00 2001 From: Elin Waring Date: Mon, 6 Jul 2026 00:47:59 -0400 Subject: [PATCH 2/3] Add test and update snapshots. --- tests/testthat/_snaps/data_tabulate.md | 70 ++++++++++++++------------ tests/testthat/test-data_tabulate.R | 4 ++ 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/tests/testthat/_snaps/data_tabulate.md b/tests/testthat/_snaps/data_tabulate.md index 872b3764c..a987b1e97 100644 --- a/tests/testthat/_snaps/data_tabulate.md +++ b/tests/testthat/_snaps/data_tabulate.md @@ -907,10 +907,11 @@ as.table(x) Output [[1]] - 3 4 5 - 4 1 8 2 - 6 2 4 1 - 8 12 0 2 + mtcars$gear + mtcars$cyl 3 4 5 + 4 1 8 2 + 6 2 4 1 + 8 12 0 2 --- @@ -918,10 +919,11 @@ Code as.table(x, simplify = TRUE) Output - 3 4 5 - 4 1 8 2 - 6 2 4 1 - 8 12 0 2 + mtcars$gear + mtcars$cyl 3 4 5 + 4 1 8 2 + 6 2 4 1 + 8 12 0 2 --- @@ -929,10 +931,11 @@ as.table(x) Output [[1]] - 3 4 5 - 4 1 8 2 - 6 2 4 1 - 8 12 0 2 + gear + cyl 3 4 5 + 4 1 8 2 + 6 2 4 1 + 8 12 0 2 --- @@ -940,10 +943,11 @@ Code as.table(x, simplify = TRUE) Output - 3 4 5 - 4 1 8 2 - 6 2 4 1 - 8 12 0 2 + gear + cyl 3 4 5 + 4 1 8 2 + 6 2 4 1 + 8 12 0 2 --- @@ -951,15 +955,17 @@ as.table(x) Output [[1]] - 3 4 5 - 0 15 4 0 - 1 0 8 5 + gear + am 3 4 5 + 0 15 4 0 + 1 0 8 5 [[2]] - 3 4 5 - 4 1 8 2 - 6 2 4 1 - 8 12 0 2 + gear + cyl 3 4 5 + 4 1 8 2 + 6 2 4 1 + 8 12 0 2 --- @@ -968,15 +974,17 @@ as.table(x) Output $`am (0)` - 3 4 - 4 1 2 - 6 2 2 - 8 12 0 + gear + cyl 3 4 + 4 1 2 + 6 2 2 + 8 12 0 $`am (1)` - 4 5 - 4 6 2 - 6 2 1 - 8 0 2 + gear + cyl 4 5 + 4 6 2 + 6 2 1 + 8 0 2 diff --git a/tests/testthat/test-data_tabulate.R b/tests/testthat/test-data_tabulate.R index 45356583e..17d397329 100644 --- a/tests/testthat/test-data_tabulate.R +++ b/tests/testthat/test-data_tabulate.R @@ -1031,6 +1031,10 @@ test_that("data_tabulate, table methods", { x <- data_tabulate(mtcars$cyl, mtcars$gear) expect_type(as.table(x), "list") expect_s3_class(as.table(x, simplify = TRUE), "table") + expect_identical( + names(dimnames(as.table(x)[[1]])), + c("mtcars$cyl", "mtcars$gear") + ) expect_snapshot(as.table(x)) expect_snapshot(as.table(x, simplify = TRUE)) From 041ef7286b2476108918b530194e2ffb649fb6a1 Mon Sep 17 00:00:00 2001 From: Elin Waring Date: Mon, 6 Jul 2026 01:44:40 -0400 Subject: [PATCH 3/3] fix lint --- tests/testthat/test-data_tabulate.R | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/testthat/test-data_tabulate.R b/tests/testthat/test-data_tabulate.R index 783163b45..c8b55e756 100644 --- a/tests/testthat/test-data_tabulate.R +++ b/tests/testthat/test-data_tabulate.R @@ -1167,10 +1167,7 @@ test_that("data_tabulate, table methods", { x <- data_tabulate(mtcars$cyl, mtcars$gear) expect_type(as.table(x), "list") expect_s3_class(as.table(x, simplify = TRUE), "table") - expect_identical( - names(dimnames(as.table(x)[[1]])), - c("mtcars$cyl", "mtcars$gear") - ) + expect_named(dimnames(as.table(x)[[1]]), c("mtcars$cyl", "mtcars$gear")) expect_snapshot(as.table(x)) expect_snapshot(as.table(x, simplify = TRUE))