Skip to content

Commit 770ec03

Browse files
Melkiadesshajoezhugithub-actions[bot]
authored
Fix bug in printing table with only one row, topleft information and column names with newlines (#326)
Fixes insightsengineering/rtables#942 --------- Co-authored-by: shajoezhu <[email protected]> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 84e8c3e commit 770ec03

File tree

4 files changed

+64
-13
lines changed

4 files changed

+64
-13
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: formatters
22
Title: ASCII Formatting for Values and Tables
3-
Version: 0.5.9.9003
3+
Version: 0.5.9.9004
44
Date: 2024-10-28
55
Authors@R: c(
66
person("Gabriel", "Becker", , "[email protected]", role = "aut",

NEWS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
## formatters 0.5.9.9003
1+
## formatters 0.5.9.9004
22
* Fixed a bug in `mf_update_cinfo` causing an error when `export_as_txt` was applied to empty listings.
3+
* Fixed a bug in `mform_handle_newlines` that did not allow printing to console a table that has top left information, new lines in the column names and contained only one row.
4+
* Fixed a bug in `mform_handle_newlines` that did not allow for empty strings to be present in top left information.
35

46
## formatters 0.5.9
57
* Fixed bug in `format_value` that caused multiple `NA` values to be associated with the wrong `na_str` values.

R/matrix_form.R

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,27 @@ mform_handle_newlines <- function(matform) {
1717
spamat <- mf_spans(matform)
1818
alimat <- mf_aligns(matform)
1919
nr_header <- mf_nrheader(matform)
20-
nl_inds_header <- seq(1, mf_nlheader(matform))
21-
hdr_inds <- 1:nr_header
20+
nl_inds_header <- seq(mf_nlheader(matform))
21+
hdr_inds <- seq(nr_header)
2222

2323
# hack that is necessary only if top-left is bottom aligned (default)
2424
topleft_has_nl_char <- FALSE
2525
if (has_topleft) {
26+
# extract topleft info
2627
tl <- strmat[nl_inds_header, 1, drop = TRUE]
28+
29+
# removes it from the header (temporary) - done so the header can be top aligned
2730
strmat[nl_inds_header, 1] <- ""
28-
tl <- tl[nzchar(tl)] # we are not interested in initial "" but we cover initial \n
31+
32+
# remove top empty strings (because they are not topleft) and assign topleft to add back
33+
if (any(nzchar(tl)) && length(tl) > 1) { # needed if ever has_top_left is true but only empties
34+
# values that are "" before topleft information
35+
to_remove <- seq(which(nzchar(tl))[1] - 1)
36+
tl <- tl[-to_remove]
37+
}
2938
topleft_has_nl_char <- any(grepl("\n", tl))
3039
tl_to_add_back <- strsplit(paste0(tl, collapse = "\n"), split = "\n", fixed = TRUE)[[1]]
31-
how_many_nl <- length(tl_to_add_back)
40+
tl_how_many_nl <- length(tl_to_add_back)
3241
}
3342

3443
# pre-proc in case of wrapping and \n
@@ -44,7 +53,7 @@ mform_handle_newlines <- function(matform) {
4453
# because we don't care about wrapping here we're counting lines
4554
# TODO probably better if we had a nlines_nowrap fun to be more explicit
4655

47-
row_nlines <- apply(
56+
row_nlines <- apply( # tells how many nlines for each row
4857
strmat,
4958
1,
5059
function(x) {
@@ -60,10 +69,9 @@ mform_handle_newlines <- function(matform) {
6069
}
6170
)
6271

63-
6472
# Correction for the case where there are more lines for topleft material than for cols
65-
if (has_topleft && (sum(row_nlines[nl_inds_header]) < how_many_nl)) {
66-
row_nlines[1] <- row_nlines[1] + how_many_nl - sum(row_nlines[nl_inds_header])
73+
if (has_topleft && (sum(row_nlines[hdr_inds]) < tl_how_many_nl)) {
74+
row_nlines[1] <- row_nlines[1] + tl_how_many_nl - sum(row_nlines[hdr_inds])
6775
}
6876

6977
# There is something to change
@@ -95,8 +103,8 @@ mform_handle_newlines <- function(matform) {
95103
)
96104

97105
if (has_topleft) {
98-
starts_from_ind <- if (sum(row_nlines[hdr_inds]) - how_many_nl > 0) {
99-
sum(row_nlines[hdr_inds]) - how_many_nl
106+
starts_from_ind <- if (sum(row_nlines[hdr_inds]) - tl_how_many_nl > 0) {
107+
sum(row_nlines[hdr_inds]) - tl_how_many_nl
100108
} else {
101109
0
102110
}
@@ -127,7 +135,6 @@ mform_handle_newlines <- function(matform) {
127135
prov_footer(matform) <- .quick_handle_nl(prov_footer(matform))
128136

129137
# xxx \n in page titles are not working atm (I think)
130-
131138
matform
132139
}
133140

tests/testthat/test-txt_wrap.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,45 @@ test_that("misc font device related test coverage", {
461461
expect_true(is_monospace(font_spec()))
462462
expect_false(is_monospace(font_spec("Times")))
463463
})
464+
465+
test_that("matrix_form is prepared correctly for printing when there is topleft information and new lines", {
466+
# Regression test for #942 in {rtables}
467+
fake_data <- data.frame("A", 0, 0)
468+
colnames(fake_data) <- c("aaaaaaaaaa", "A: \n\nDrug X", "B: \nPlacebo")
469+
bmf <- basic_matrix_form(fake_data, ignore_rownames = TRUE)
470+
bmf$has_topleft <- TRUE
471+
472+
expect_silent(printed_out <- strsplit(toString(bmf, hsep = "-"), "\n")[[1]])
473+
expect_equal(
474+
printed_out,
475+
c(
476+
" A: ",
477+
" B: ",
478+
"aaaaaaaaaa Drug X Placebo",
479+
"-----------------------------",
480+
" A 0 0 "
481+
)
482+
)
483+
484+
# Another test for complex topleft information
485+
colnames(fake_data) <- c("\n\naaaaaaaaaa\n\nb", "A: \n\nDrug X", "B: \nPlacebo")
486+
bmf <- basic_matrix_form(fake_data, ignore_rownames = TRUE)
487+
bmf$has_topleft <- TRUE
488+
bmf$aligns[, 1] <- "left"
489+
490+
expect_equal(bmf$strings[, 1], c("", "", "aaaaaaaaaa", "", "b", "A"))
491+
492+
expect_silent(printed_out <- strsplit(toString(bmf, hsep = "-"), "\n")[[1]])
493+
expect_equal(
494+
printed_out,
495+
c(
496+
" ",
497+
" ",
498+
"aaaaaaaaaa A: ",
499+
" B: ",
500+
"b Drug X Placebo",
501+
"-----------------------------",
502+
"A 0 0 "
503+
)
504+
)
505+
})

0 commit comments

Comments
 (0)