From 79c90819656e6093d6f21398f0cab698351db5a8 Mon Sep 17 00:00:00 2001 From: Melkiades Date: Wed, 8 Nov 2023 11:08:25 +0100 Subject: [PATCH 1/8] The fix --- R/tostring.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/R/tostring.R b/R/tostring.R index 34f50f324..24271022e 100644 --- a/R/tostring.R +++ b/R/tostring.R @@ -500,7 +500,7 @@ setMethod("toString", "MatrixPrintForm", function(x, aligns <- mf_aligns(mat) keep_mat <- mf_display(mat) ## spans <- mat$spans - ## ri <- mat$row_info + mf_ri <- mf_rinfo(mat) ref_fnotes <- mf_rfnotes(mat) nl_header <- mf_nlheader(mat) @@ -523,12 +523,12 @@ setMethod("toString", "MatrixPrintForm", function(x, txt_head <- apply(head(content, nl_header), 1, .paste_no_na, collapse = gap_str) # txt body - sec_seps_df <- x$row_info[, c("abs_rownumber", "trailing_sep"), drop = FALSE] + sec_seps_df <- mf_ri[, c("abs_rownumber", "trailing_sep"), drop = FALSE] if (!is.null(sec_seps_df) && any(!is.na(sec_seps_df$trailing_sep))) { bdy_cont <- tail(content, -nl_header) ## unfortunately we count "header rows" wrt line grouping so it ## doesn't match the real (i.e. body) rows as is - row_grouping <- tail(x$line_grouping, -nl_header) - mf_nrheader(x) + row_grouping <- tail(mf_lgrouping(mat), - nl_header) - mf_nrheader(mat) nrbody <- NROW(bdy_cont) stopifnot(length(row_grouping) == nrbody) ## all rows with non-NA section divs and the final row (regardless of NA status) @@ -569,7 +569,7 @@ setMethod("toString", "MatrixPrintForm", function(x, } # retrieving titles and footers - allts <- all_titles(x) + allts <- all_titles(mat) # Fix for ref_fnotes with \n characters XXX this does not count in the pagination if (any(grepl("\n", ref_fnotes))) { @@ -577,8 +577,8 @@ setMethod("toString", "MatrixPrintForm", function(x, } allfoots <- list( - "main_footer" = main_footer(x), - "prov_footer" = prov_footer(x), + "main_footer" = main_footer(mat), + "prov_footer" = prov_footer(mat), "ref_footnotes" = ref_fnotes ) allfoots <- allfoots[!sapply(allfoots, is.null)] From 9551f069ce57a0161a30974c54a5fa3886a5f20b Mon Sep 17 00:00:00 2001 From: Melkiades Date: Fri, 10 Nov 2023 08:28:42 +0100 Subject: [PATCH 2/8] test --- tests/testthat/test-txt_wrap.R | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/testthat/test-txt_wrap.R b/tests/testthat/test-txt_wrap.R index 73265950a..6340a5247 100644 --- a/tests/testthat/test-txt_wrap.R +++ b/tests/testthat/test-txt_wrap.R @@ -280,3 +280,17 @@ test_that("toString wrapping avoid trimming whitespaces", { res ) }) + +test_that("toString and wrapping cooperates well with separator divisors", { + testdf <- iris[seq_len(5), seq_len(2)] + rownames(testdf) <- paste("State ", LETTERS[seq_len(nrow(testdf))]) + bmf <- basic_matrix_form(testdf) + + # Adding topleft to wrap + bmf$has_topleft <- TRUE # no setter atm + mf_strings(bmf)[1, 1] <- "LETTERS" + + sec_seps_df <- mf_rinfo(bmf)[, c("abs_rownumber", "trailing_sep"), drop = FALSE] + mf_rinfo(bmf)$trailing_sep[c(1, 3, 4)] <- " " + toString(bmf, widths = c(4, 4, 4)) %>% cat +}) From 8d8f4a7620ca8934969f70d8e269fc59fbe457d2 Mon Sep 17 00:00:00 2001 From: Melkiades Date: Fri, 10 Nov 2023 08:32:24 +0100 Subject: [PATCH 3/8] fix test --- tests/testthat/test-txt_wrap.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-txt_wrap.R b/tests/testthat/test-txt_wrap.R index 6340a5247..2ebfd6355 100644 --- a/tests/testthat/test-txt_wrap.R +++ b/tests/testthat/test-txt_wrap.R @@ -292,5 +292,5 @@ test_that("toString and wrapping cooperates well with separator divisors", { sec_seps_df <- mf_rinfo(bmf)[, c("abs_rownumber", "trailing_sep"), drop = FALSE] mf_rinfo(bmf)$trailing_sep[c(1, 3, 4)] <- " " - toString(bmf, widths = c(4, 4, 4)) %>% cat + expect_silent(toString(bmf, widths = c(4, 4, 4))) }) From 5e0a6a9a21d59591dbe90584be6848b8dec6776d Mon Sep 17 00:00:00 2001 From: Melkiades Date: Fri, 10 Nov 2023 10:36:41 +0100 Subject: [PATCH 4/8] fixes 221 --- tests/testthat/test-txt_wrap.R | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testthat/test-txt_wrap.R b/tests/testthat/test-txt_wrap.R index 2ebfd6355..b557264a8 100644 --- a/tests/testthat/test-txt_wrap.R +++ b/tests/testthat/test-txt_wrap.R @@ -282,6 +282,7 @@ test_that("toString wrapping avoid trimming whitespaces", { }) test_that("toString and wrapping cooperates well with separator divisors", { + # Fixes #221 testdf <- iris[seq_len(5), seq_len(2)] rownames(testdf) <- paste("State ", LETTERS[seq_len(nrow(testdf))]) bmf <- basic_matrix_form(testdf) From e8f92eaa9818ff9910a98e109e2dc9e7b0b46124 Mon Sep 17 00:00:00 2001 From: Melkiades Date: Tue, 21 Nov 2023 17:59:36 +0100 Subject: [PATCH 5/8] allowing header_section_div --- R/generics.R | 19 +++++++++++++++++++ R/matrix_form.R | 4 ++++ R/tostring.R | 8 +++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/R/generics.R b/R/generics.R index bafaee5f7..7410c1f95 100644 --- a/R/generics.R +++ b/R/generics.R @@ -639,3 +639,22 @@ setGeneric("num_rep_cols", function(obj) standardGeneric("num_rep_cols")) #' @export #' @rdname num_rep_cols setMethod("num_rep_cols", "ANY", function(obj) 0L) + +# header_section_div ----------------------------------------------------------- +#' @keywords internal +setGeneric("header_section_div", function(obj) standardGeneric("header_section_div")) +#' @keywords internal +setMethod( + "header_section_div", "MatrixPrintForm", + function(obj) obj$header_section_div +) +#' @keywords internal +setGeneric("header_section_div<-", function(obj, value) standardGeneric("header_section_div<-")) +#' @keywords internal +setMethod( + "header_section_div<-", "MatrixPrintForm", + function(obj, value) { + obj$header_section_div <- value + obj + } +) diff --git a/R/matrix_form.R b/R/matrix_form.R index 05c8b261c..7d9c2249f 100644 --- a/R/matrix_form.R +++ b/R/matrix_form.R @@ -193,6 +193,8 @@ disp_from_spans <- function(spans) { #' @param main_footer character(1). Main footer as a string. #' @param prov_footer character. Provenance footer information as a #' character vector. +#' @param header_section_div character(1). Divider to be used between header +#' and body sections. #' @param expand_newlines logical(1). Should the matrix form generated #' expand rows whose values contain newlines into multiple #' 'physical' rows (as they will appear when rendered into @@ -252,6 +254,7 @@ MatrixPrintForm <- function(strings = NULL, page_titles = character(), main_footer = "", prov_footer = character(), + header_section_div = NA_character_, col_gap = 3, table_inset = 0L, colwidths = NULL, @@ -274,6 +277,7 @@ MatrixPrintForm <- function(strings = NULL, page_titles = page_titles, main_footer = main_footer, prov_footer = prov_footer, + header_section_div = header_section_div, col_gap = col_gap, table_inset = as.integer(table_inset), has_topleft = has_topleft, diff --git a/R/tostring.R b/R/tostring.R index 24271022e..6c1d711b8 100644 --- a/R/tostring.R +++ b/R/tostring.R @@ -518,6 +518,12 @@ setMethod("toString", "MatrixPrintForm", function(x, # Define gap string and divisor string gap_str <- strrep(" ", col_gap) div <- substr(strrep(hsep, ncchar), 1, ncchar) + hsd <- header_section_div(mat) + if (!is.na(hsd)) { + hsd <- substr(strrep(hsd, ncchar), 1, ncchar) + } else { + hsd <- NULL # no divisor + } # text head (paste w/o NA content header and gap string) txt_head <- apply(head(content, nl_header), 1, .paste_no_na, collapse = gap_str) @@ -605,6 +611,7 @@ setMethod("toString", "MatrixPrintForm", function(x, titles_txt, # .do_inset(div, inset) happens if there are any titles .do_inset(txt_head, inset), .do_inset(div, inset), + .do_inset(hsd, inset), # header_section_div if present .do_inset(txt_body, inset), .footer_inset_helper(allfoots, div, inset) ), collapse = "\n"), @@ -977,7 +984,6 @@ spans_to_viscell <- function(spans) { ## ' mf <- matrix_form(tbl) ## ' propose_column_widths(mf) propose_column_widths <- function(x, indent_size = 2) { - ## stopifnot(is(x, "VTableTree")) if (!is(x, "MatrixPrintForm")) { x <- matrix_form(x, indent_rownames = TRUE, indent_size = indent_size) } From b1ffe20c375e28340be3058f9f9e0d5506cf87a5 Mon Sep 17 00:00:00 2001 From: Melkiades Date: Tue, 21 Nov 2023 18:01:38 +0100 Subject: [PATCH 6/8] news --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 738adef3e..4d78e17d5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ ## formatters 0.5.4.9002 * Applied `styler` and resolved package lint. Changed default indentation from 4 spaces to 2. + * Fixed wrapping and section dividers error. + * Allowed sectiond divider between header and table body. ## formatters 0.5.4 * Fixed a bug in `paginate_to_mpfs()` so that formatting in listings key columns is retained with pagination [`insightsengineering/rlistings#155`](https://github.com/insightsengineering/rlistings/issues/155). From 1b299211f49e25c891912783e44d34f33e8fd5c4 Mon Sep 17 00:00:00 2001 From: "27856297+dependabot-preview[bot]@users.noreply.github.com" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Tue, 21 Nov 2023 17:03:32 +0000 Subject: [PATCH 7/8] [skip actions] Roxygen Man Pages Auto Update --- man/MatrixPrintForm.Rd | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/man/MatrixPrintForm.Rd b/man/MatrixPrintForm.Rd index 4fa7c739a..58a03716b 100644 --- a/man/MatrixPrintForm.Rd +++ b/man/MatrixPrintForm.Rd @@ -23,6 +23,7 @@ MatrixPrintForm( page_titles = character(), main_footer = "", prov_footer = character(), + header_section_div = NA_character_, col_gap = 3, table_inset = 0L, colwidths = NULL, @@ -47,6 +48,7 @@ matrix_print_form( page_titles = character(), main_footer = "", prov_footer = character(), + header_section_div = NA_character_, col_gap = 3, table_inset = 0L, colwidths = NULL, @@ -120,6 +122,9 @@ vector.} \item{prov_footer}{character. Provenance footer information as a character vector.} +\item{header_section_div}{character(1). Divider to be used between header +and body sections.} + \item{col_gap}{numeric(1). Space (in characters) between columns} \item{table_inset}{numeric(1). Table inset. See From 6e07ef65a8a21cd95f124b9a2d7e5a0c3e770e44 Mon Sep 17 00:00:00 2001 From: Davide Garolini Date: Tue, 21 Nov 2023 18:04:26 +0100 Subject: [PATCH 8/8] Update NEWS.md Signed-off-by: Davide Garolini --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 4d78e17d5..c822609ab 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,7 @@ ## formatters 0.5.4.9002 * Applied `styler` and resolved package lint. Changed default indentation from 4 spaces to 2. * Fixed wrapping and section dividers error. - * Allowed sectiond divider between header and table body. + * Allowed section divider between header and table body. ## formatters 0.5.4 * Fixed a bug in `paginate_to_mpfs()` so that formatting in listings key columns is retained with pagination [`insightsengineering/rlistings#155`](https://github.com/insightsengineering/rlistings/issues/155).