From a7010e38fed4e4384549623389ebd14b4e3427f8 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Fri, 9 May 2025 23:18:25 +0000 Subject: [PATCH 1/2] Use drop=FALSE to avoid losing dimension in double-elided case --- R/repr_matrix_df.r | 6 ++++-- tests/testthat/test_repr_array_df.r | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/R/repr_matrix_df.r b/R/repr_matrix_df.r index af8dcbe..f5b2730 100644 --- a/R/repr_matrix_df.r +++ b/R/repr_matrix_df.r @@ -43,8 +43,10 @@ arr_partition <- function(a, rows, cols) { # assign a list of parts that can be coerced to strings if (!is.null(part_r) && !is.null(part_c)) { structure(list( - ul = a[part_r$start, part_c$start], ll = a[part_r$end, part_c$start], - ur = a[part_r$start, part_c$end ], lr = a[part_r$end, part_c$end ]), + ul = a[part_r$start, part_c$start, drop = FALSE], + ll = a[part_r$end , part_c$start, drop = FALSE], + ur = a[part_r$start, part_c$end, drop = FALSE], + lr = a[part_r$end , part_c$end, drop = FALSE]), omit = 'both') } else if (!is.null(part_r)) { structure(list( diff --git a/tests/testthat/test_repr_array_df.r b/tests/testthat/test_repr_array_df.r index 816e8b0..f34f188 100644 --- a/tests/testthat/test_repr_array_df.r +++ b/tests/testthat/test_repr_array_df.r @@ -204,3 +204,25 @@ test_that('data.frame with list columns can be displayed', { expect_identical(repr_html(data.table::as.data.table(df)), sub('data\\.frame','data.table',expected)) } }) + +test_that('forced-narrow inputs work', { + withr::local_options(repr.matrix.max.rows = 2L, repr.matrix.max.cols = 2L) + df <- data.frame(a = 1:3, b = 4:6, c = 7:9) + expect_silent(repr_text(df)) + expect_identical( + # Scrub non-ASCII characters to make the test platform-agnostic. + gsub("[^a-zA-Z0-9.&;<>= '\"/:\n\t]", "*", repr_html(df)), + " + + +\t +\t + + +\t +\t +\t + +
A data.frame: 3 * 3
a*c
<int>*<int>
1*7
***
3*9
+") +}) From 9d14b1636f39ab654163aeaab38cf9f80e2b0a73 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Fri, 9 May 2025 16:33:08 -0700 Subject: [PATCH 2/2] correct local_options() call --- tests/testthat/test_repr_array_df.r | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test_repr_array_df.r b/tests/testthat/test_repr_array_df.r index f34f188..2d5c726 100644 --- a/tests/testthat/test_repr_array_df.r +++ b/tests/testthat/test_repr_array_df.r @@ -206,7 +206,7 @@ test_that('data.frame with list columns can be displayed', { }) test_that('forced-narrow inputs work', { - withr::local_options(repr.matrix.max.rows = 2L, repr.matrix.max.cols = 2L) + withr::local_options(list(repr.matrix.max.rows = 2L, repr.matrix.max.cols = 2L)) df <- data.frame(a = 1:3, b = 4:6, c = 7:9) expect_silent(repr_text(df)) expect_identical(