From bf9364e11e8e46c055017fe7568f762c6cfb0c0c Mon Sep 17 00:00:00 2001 From: Bill Denney Date: Sun, 4 Oct 2026 12:59:00 -0400 Subject: [PATCH 1/2] feat: name the column and the cell text when a row is too tall for the page The row-overflow message now names the column holding the row's tallest cell and quotes the first 40 characters of that cell (line breaks as spaces, an ellipsis after a longer text), as the width messages already name the column. The error has class writetfl_error_row_too_tall and the overflow_action = "warn" warning writetfl_warning_row_too_tall. Co-Authored-By: Claude Sonnet 5.5 --- R/layout.R | 7 ++- R/table_rows.R | 41 ++++++++++---- design/ARCHITECTURE.md | 3 + design/DECISIONS.md | 30 ++++++++++ man/paginate_rows.Rd | 3 + tests/testthat/test-tfl_table.R | 98 +++++++++++++++++++++++++++++++++ 6 files changed, 169 insertions(+), 13 deletions(-) diff --git a/R/layout.R b/R/layout.R index c159676..7555d33 100644 --- a/R/layout.R +++ b/R/layout.R @@ -43,15 +43,16 @@ check_content_height <- function(content_h_in, min_content_height, errors) { # Shared dispatch for width-overflow events. Either appends `msg` to `errors` # (when `overflow_action == "error"`) or emits an immediate rlang::warn() (when # `"warn"`) and returns `errors` unchanged. Every overflow message ends with -# the diagnostic-mode hint so users always see the escape hatch. -.overflow_signal <- function(msg, overflow_action, errors) { +# the diagnostic-mode hint so users always see the escape hatch. `class` is +# the condition class of the warning (the caller gives the abort its own). +.overflow_signal <- function(msg, overflow_action, errors, class = NULL) { msg <- paste0( msg, "\n Set `overflow_action = \"warn\"` to convert this error to a ", "warning and still produce output for diagnosis." ) if (identical(overflow_action, "warn")) { - rlang::warn(msg) + rlang::warn(msg, class = class) errors } else { c(errors, msg) diff --git a/R/table_rows.R b/R/table_rows.R index 3207b6e..091769f 100644 --- a/R/table_rows.R +++ b/R/table_rows.R @@ -213,6 +213,24 @@ measure_row_heights_tbl <- function(data, resolved_cols, gp_tbl, cell_padding, # paginate_rows() — group-aware row pagination # --------------------------------------------------------------------------- +# The beginning of a cell's text for an overflow message: line breaks shown as +# single spaces, cut to `n` characters and followed by "..." when longer. A +# missing value reads "NA"; a list cell shows its unlisted values. +.cell_text_prefix <- function(x, n = 40L) { + txt <- as.character(unlist(x)) + txt[is.na(txt)] <- "NA" + txt <- gsub("\r\n|\r|\n", " ", paste(txt, collapse = " ")) + if (nchar(txt) > n) paste0(substr(txt, 1L, n), "...") else txt +} + +# Index of the column that makes a row too tall: the wrap-eligible column whose +# cell has the greatest measured height, or the tallest cell overall when no +# wrap-eligible cell has any height. +.bottleneck_col <- function(cell_heights, wrap_elig) { + cand_h <- cell_heights * as.numeric(wrap_elig) + if (any(cand_h > 0)) which.max(cand_h) else which.max(cell_heights) +} + #' Split rows into pages, respecting group boundaries #' #' Uses a per-page tentative recompute of `.compute_page_row_heights()` so that @@ -246,6 +264,9 @@ measure_row_heights_tbl <- function(data, resolved_cols, gp_tbl, cell_padding, #' exceeds the available page content height (a row that wraps to taller #' than one page is almost always a sign of input that needs to change). #' The same knob downgrades column-overflow events; see [export_tfl_page()]. +#' The error has class `writetfl_error_row_too_tall` and the warning +#' `writetfl_warning_row_too_tall`; the message names the row, the column +#' holding its tallest cell, and the first 40 characters of that cell. #' Ignored when `collect_overflows = TRUE`. #' @param collect_overflows Logical. When `FALSE` (default) the function #' behaves as before: row-overflow events are routed through @@ -345,28 +366,28 @@ paginate_rows <- function(data, cell_h_mat, resolved_cols, group_vars, sum(rh) if (min_required > content_height_in + 1e-6) { if (collect_overflows) { - # Identify the bottleneck column: the wrap-eligible column whose - # cell in row i has the greatest measured height. Fall back to - # the tallest cell overall if no wrap-eligible column is present - # in this row (the caller's retry loop will still know which row - # was the problem). cell_heights <- cell_h_mat[i, ] - cand_h <- cell_heights * as.numeric(wrap_elig) - bot_j <- if (any(cand_h > 0)) which.max(cand_h) else which.max(cell_heights) + bot_j <- .bottleneck_col(cell_heights, wrap_elig) overflows[[length(overflows) + 1L]] <- list( row = i, bottleneck_col = bot_j, cell_height_in = cell_heights[[bot_j]] ) } else { + bot_col <- resolved_cols[[.bottleneck_col(cell_h_mat[i, ], wrap_elig)]]$col msg <- sprintf( paste0("Row %d of the table wraps to a height (%.3g in) that ", "exceeds the available page content height (%.3g in). ", + "The tallest cell is in column '%s' and starts \"%s\". ", "Reduce the cell content, increase the page height, widen ", "the column, or set the column to wrap less aggressively."), - i, sum(rh), content_height_in + i, sum(rh), content_height_in, + bot_col, .cell_text_prefix(data[[bot_col]][i]) + ) + errors <- .overflow_signal( + msg, overflow_action, errors, + class = "writetfl_warning_row_too_tall" ) - errors <- .overflow_signal(msg, overflow_action, errors) } } # Fall through to commit the row. @@ -387,7 +408,7 @@ paginate_rows <- function(data, cell_h_mat, resolved_cols, group_vars, } if (length(errors) > 0L) { - rlang::abort(paste(errors, collapse = "\n")) + rlang::abort(paste(errors, collapse = "\n"), class = "writetfl_error_row_too_tall") } pages diff --git a/design/ARCHITECTURE.md b/design/ARCHITECTURE.md index 744db68..adf0934 100644 --- a/design/ARCHITECTURE.md +++ b/design/ARCHITECTURE.md @@ -737,6 +737,9 @@ paginate_rows(data, cell_h_mat, resolved_cols, group_vars, A group split across the boundary signals an rlang warning of class `writetfl_warning_group_spans_pages` (the page already carries the continuation marker); callers that expect it match the class, not the text. + A single row taller than the page signals `writetfl_error_row_too_tall` + (`writetfl_warning_row_too_tall` under `overflow_action = "warn"`), naming + the row, the bottleneck column and the first 40 characters of its cell. ``` --- diff --git a/design/DECISIONS.md b/design/DECISIONS.md index 1f983f5..9dfa3b2 100644 --- a/design/DECISIONS.md +++ b/design/DECISIONS.md @@ -2336,3 +2336,33 @@ silencing and nothing fails. A class is a stable handle. **Tests:** `tests/testthat/test-tfl_table.R` (the warning has the class, and an export that splits a group signals only that class). + + +--- + +## D-56: The row-too-tall message names the column and the start of the cell + +**Decision:** When a single row wraps to taller than the page content height, +`paginate_rows()` names the row, the column holding its tallest cell (the same +bottleneck column the row-overflow retry loop uses: the tallest wrap-eligible +cell, else the tallest cell), and the first 40 characters of that cell's text, +with line breaks shown as spaces and `...` after a longer text. The error has +class `writetfl_error_row_too_tall`, and under `overflow_action = "warn"` the +warning has class `writetfl_warning_row_too_tall`; both carry the same message. +The width messages already name the column; this brings the height message to +the same level. + +**Motivation:** "Row 412 of the table" is not enough in a table with sub-tables, +group columns and column pages. The column and the start of the offending text +let the row be found by searching the source data. The text is cut at 40 +characters so a very long cell (the usual cause) does not flood the message. + +**Alternatives rejected:** quoting the whole cell (the cell is the problem +because it is long); naming every column that wraps past the page (only the +tallest decides the row height); a group value or row key instead of cell text +(a table has no row key, and the cell text is what a reader can search for). + +**Tests:** `tests/testthat/test-tfl_table.R` (`.cell_text_prefix()` truncation +pinned at the 40-character boundary, line breaks, `NA` and list cells; +`.bottleneck_col()`; the error and the warning from `export_tfl()` name row, +column and text prefix; `paginate_rows()` picks the tallest column). diff --git a/man/paginate_rows.Rd b/man/paginate_rows.Rd index 04c3594..91cd742 100644 --- a/man/paginate_rows.Rd +++ b/man/paginate_rows.Rd @@ -47,6 +47,9 @@ the row-overflow guard reports a single row whose committed height exceeds the available page content height (a row that wraps to taller than one page is almost always a sign of input that needs to change). The same knob downgrades column-overflow events; see \code{\link[=export_tfl_page]{export_tfl_page()}}. +The error has class \code{writetfl_error_row_too_tall} and the warning +\code{writetfl_warning_row_too_tall}; the message names the row, the column +holding its tallest cell, and the first 40 characters of that cell. Ignored when \code{collect_overflows = TRUE}.} \item{collect_overflows}{Logical. When \code{FALSE} (default) the function diff --git a/tests/testthat/test-tfl_table.R b/tests/testthat/test-tfl_table.R index 5363ada..0bd67c7 100644 --- a/tests/testthat/test-tfl_table.R +++ b/tests/testthat/test-tfl_table.R @@ -1663,3 +1663,101 @@ test_that("tfl_table() rejects an NA column label up front", { expect_error(tfl_table(d, col_labels = c(a = NA_character_)), "col_labels.*NA") expect_no_error(tfl_table(d, col_labels = c(a = "A"))) }) + +# --------------------------------------------------------------------------- +# Row-overflow message names the column and the cell text (issue 44) +# --------------------------------------------------------------------------- + +test_that(".cell_text_prefix keeps up to 40 characters and cuts longer text with an ellipsis", { + forty <- "0123456789012345678901234567890123456789" + expect_equal(nchar(forty), 40L) + expect_equal(.cell_text_prefix(forty), forty) + expect_equal(.cell_text_prefix(paste0(forty, "x")), paste0(forty, "...")) + expect_equal(.cell_text_prefix(paste0(forty, "xyz and a lot more")), paste0(forty, "...")) + expect_equal(.cell_text_prefix("short"), "short") + expect_equal(.cell_text_prefix("", n = 5L), "") + expect_equal(.cell_text_prefix("abcdefgh", n = 5L), "abcde...") +}) + +test_that(".cell_text_prefix shows each line break as one space, and handles NA and list cells", { + expect_equal(.cell_text_prefix("one\ntwo\r\nthree\rfour"), "one two three four") + expect_equal(.cell_text_prefix("a\n\nb"), "a b") + expect_equal(.cell_text_prefix(NA_character_), "NA") + expect_equal(.cell_text_prefix(12.5), "12.5") + expect_equal(.cell_text_prefix(list(c("x", "y"))), "x y") + # The line break counts as a character, after it is made a space. + expect_equal(.cell_text_prefix("abc\ndef", n = 5L), "abc d...") +}) + +test_that(".bottleneck_col picks the tallest wrap-eligible cell, else the tallest cell", { + expect_equal(.bottleneck_col(c(1, 5, 3), c(TRUE, FALSE, TRUE)), 3L) + expect_equal(.bottleneck_col(c(4, 2, 3), c(TRUE, TRUE, TRUE)), 1L) + # No wrap-eligible column: fall back to the tallest cell overall. + expect_equal(.bottleneck_col(c(1, 5, 3), c(FALSE, FALSE, FALSE)), 2L) + # A wrap-eligible column with no height does not count. + expect_equal(.bottleneck_col(c(1, 5, 0), c(FALSE, FALSE, TRUE)), 2L) +}) + +row_overflow_table <- function() { + essay <- paste( + "The quick brown fox jumps over the lazy dog and keeps running.", + paste(rep("aa bb cc dd ee ff", 360), collapse = " ") + ) + df <- data.frame( + id = c("short row", "tall row"), + notes = c("fine", essay), + stringsAsFactors = FALSE + ) + tfl_table( + df, + cols = list(tfl_colspec("notes", width = grid::unit(0.8, "inches"), wrap = TRUE)) + ) +} + +test_that("a row too tall for the page errors naming the row, the column and the start of the cell", { + f <- tempfile(fileext = ".pdf") + on.exit(unlink(f)) + err <- expect_error( + export_tfl(row_overflow_table(), file = f, pg_width = 4, pg_height = 8.5, + min_content_height = grid::unit(0.5, "inches")), + class = "writetfl_error_row_too_tall" + ) + msg <- conditionMessage(err) + expect_match(msg, "Row 2 of the table wraps to a height", fixed = TRUE) + expect_match(msg, "in column 'notes'", fixed = TRUE) + expect_match(msg, "starts \"The quick brown fox jumps over the lazy ...\"", fixed = TRUE) + expect_false(grepl("short row", msg, fixed = TRUE)) +}) + +test_that("a row too tall for the page warns with the same message and a warning class under overflow_action = 'warn'", { + f <- tempfile(fileext = ".pdf") + on.exit(unlink(f)) + warn <- expect_warning( + export_tfl(row_overflow_table(), file = f, pg_width = 4, pg_height = 8.5, + min_content_height = grid::unit(0.5, "inches"), + overflow_action = "warn"), + class = "writetfl_warning_row_too_tall" + ) + msg <- conditionMessage(warn) + expect_match(msg, "Row 2 of the table wraps to a height", fixed = TRUE) + expect_match(msg, "in column 'notes'", fixed = TRUE) + expect_match(msg, "starts \"The quick brown fox jumps over the lazy ...\"", fixed = TRUE) + expect_true(file.exists(f)) +}) + +test_that("paginate_rows names the tallest wrap-eligible column of the overflowing row", { + data <- data.frame(a = c("x", "AAAA"), b = c("y", "BBBB"), stringsAsFactors = FALSE) + resolved_cols <- list( + list(col = "a", is_group_col = FALSE, wrap = TRUE), + list(col = "b", is_group_col = FALSE, wrap = TRUE) + ) + # Row 2 is 6 + 9 inches tall; column b holds the tallest cell (9). + cell_h_mat <- rbind(c(0.2, 0.2), c(6, 9)) + err <- expect_error( + paginate_rows(data, cell_h_mat, resolved_cols, group_vars = character(0), + cont_row_h = 0.2, header_row_h = 0.2, content_height_in = 3, + row_cont_msg = "(continued)", group_rule = FALSE), + class = "writetfl_error_row_too_tall" + ) + expect_match(conditionMessage(err), "in column 'b' and starts \"BBBB\"", fixed = TRUE) +}) From cbf1b96c80ce6205be2dd41d37bc07975a6805bf Mon Sep 17 00:00:00 2001 From: Bill Denney Date: Sun, 4 Oct 2026 13:07:12 -0400 Subject: [PATCH 2/2] fix: quote the row-too-tall cell as the table shows it, and cover the edge cases The cell prefix goes through the same formatting the table is drawn with, so a missing cell reads as the table's na_string and a list cell as it is drawn. The troubleshooting vignette shows the new message, and tests pin ties, multibyte text, line breaks at the limit, factors, list cells and that the width-overflow conditions carry no row-too-tall class. Co-Authored-By: Claude Sonnet 5.5 --- R/table_pagelist.R | 3 +- R/table_rows.R | 31 ++++--- design/DECISIONS.md | 2 +- man/paginate_rows.Rd | 13 ++- tests/testthat/test-tfl_table.R | 149 ++++++++++++++++++++++++------ vignettes/v04-troubleshooting.Rmd | 15 ++- 6 files changed, 165 insertions(+), 48 deletions(-) diff --git a/R/table_pagelist.R b/R/table_pagelist.R index 1266257..7d0839f 100644 --- a/R/table_pagelist.R +++ b/R/table_pagelist.R @@ -248,7 +248,8 @@ tfl_table_to_pagelist <- function(tbl, pg_width, pg_height, dots, cont_row_h, header_row_h, ch, tbl$row_cont_msg, tbl$group_rule, suppress_repeated_groups = isTRUE(tbl$suppress_repeated_groups), - collect_overflows = collect_overflows + collect_overflows = collect_overflows, + na_string = tbl$na_string ) if (!collect_overflows) { pr_args$overflow_action <- overflow_action diff --git a/R/table_rows.R b/R/table_rows.R index 091769f..eae8328 100644 --- a/R/table_rows.R +++ b/R/table_rows.R @@ -213,13 +213,12 @@ measure_row_heights_tbl <- function(data, resolved_cols, gp_tbl, cell_padding, # paginate_rows() — group-aware row pagination # --------------------------------------------------------------------------- -# The beginning of a cell's text for an overflow message: line breaks shown as -# single spaces, cut to `n` characters and followed by "..." when longer. A -# missing value reads "NA"; a list cell shows its unlisted values. -.cell_text_prefix <- function(x, n = 40L) { - txt <- as.character(unlist(x)) - txt[is.na(txt)] <- "NA" - txt <- gsub("\r\n|\r|\n", " ", paste(txt, collapse = " ")) +# The beginning of a cell's text for an overflow message, as the table shows +# it (an NA is `na_string`, anything else goes through `.fmt_cell()`): line +# breaks shown as single spaces, cut to `n` characters and followed by "..." +# when longer. +.cell_text_prefix <- function(val, na_string, n = 40L) { + txt <- gsub("\r\n|\r|\n", " ", .fmt_cell(val, na_string)) if (nchar(txt) > n) paste0(substr(txt, 1L, n), "...") else txt } @@ -264,9 +263,10 @@ measure_row_heights_tbl <- function(data, resolved_cols, gp_tbl, cell_padding, #' exceeds the available page content height (a row that wraps to taller #' than one page is almost always a sign of input that needs to change). #' The same knob downgrades column-overflow events; see [export_tfl_page()]. -#' The error has class `writetfl_error_row_too_tall` and the warning -#' `writetfl_warning_row_too_tall`; the message names the row, the column -#' holding its tallest cell, and the first 40 characters of that cell. +#' A row-too-tall error has class `writetfl_error_row_too_tall` and the +#' warning `writetfl_warning_row_too_tall`; the message names the row, the +#' column holding its tallest cell, and the first 40 characters of that cell +#' as the table shows it. #' Ignored when `collect_overflows = TRUE`. #' @param collect_overflows Logical. When `FALSE` (default) the function #' behaves as before: row-overflow events are routed through @@ -274,6 +274,8 @@ measure_row_heights_tbl <- function(data, resolved_cols, gp_tbl, cell_padding, #' overflow but instead collects the events and returns them alongside #' the page specs, so a caller can iterate (see the row-overflow retry #' loop in `.tfl_table_to_pagelist_default()`). +#' @param na_string Text shown for a missing cell, as in `tfl_table()`; used to +#' quote the cell in the row-too-tall message. #' @return When `collect_overflows = FALSE` (default), a list of row-page #' specs, each with `$rows`, `$is_cont_top`, `$is_cont_bottom`, #' `$group_starts`, and `$row_heights_in` (the committed per-row heights @@ -288,7 +290,8 @@ paginate_rows <- function(data, cell_h_mat, resolved_cols, group_vars, row_cont_msg, group_rule, suppress_repeated_groups = TRUE, overflow_action = "error", - collect_overflows = FALSE) { + collect_overflows = FALSE, + na_string = "") { n_rows <- nrow(data) # Group boundaries in the *full* data — used for the page-spec $group_starts @@ -374,6 +377,10 @@ paginate_rows <- function(data, cell_h_mat, resolved_cols, group_vars, cell_height_in = cell_heights[[bot_j]] ) } else { + # The column is chosen from the measured heights, so a suppressed + # group cell can be named, and beyond `max_measure_rows` the + # matrix holds per-column maxima, so the quoted cell may not be + # this row's tallest. bot_col <- resolved_cols[[.bottleneck_col(cell_h_mat[i, ], wrap_elig)]]$col msg <- sprintf( paste0("Row %d of the table wraps to a height (%.3g in) that ", @@ -382,7 +389,7 @@ paginate_rows <- function(data, cell_h_mat, resolved_cols, group_vars, "Reduce the cell content, increase the page height, widen ", "the column, or set the column to wrap less aggressively."), i, sum(rh), content_height_in, - bot_col, .cell_text_prefix(data[[bot_col]][i]) + bot_col, .cell_text_prefix(data[[bot_col]][i], na_string) ) errors <- .overflow_signal( msg, overflow_action, errors, diff --git a/design/DECISIONS.md b/design/DECISIONS.md index 9dfa3b2..4520c4f 100644 --- a/design/DECISIONS.md +++ b/design/DECISIONS.md @@ -2346,7 +2346,7 @@ export that splits a group signals only that class). `paginate_rows()` names the row, the column holding its tallest cell (the same bottleneck column the row-overflow retry loop uses: the tallest wrap-eligible cell, else the tallest cell), and the first 40 characters of that cell's text, -with line breaks shown as spaces and `...` after a longer text. The error has +with line breaks shown as spaces and `...` after a longer text. The text is the cell as the table shows it (a missing value is the table's `na_string`, other values go through the same formatting as drawing). The error has class `writetfl_error_row_too_tall`, and under `overflow_action = "warn"` the warning has class `writetfl_warning_row_too_tall`; both carry the same message. The width messages already name the column; this brings the height message to diff --git a/man/paginate_rows.Rd b/man/paginate_rows.Rd index 91cd742..971b0e4 100644 --- a/man/paginate_rows.Rd +++ b/man/paginate_rows.Rd @@ -16,7 +16,8 @@ paginate_rows( group_rule, suppress_repeated_groups = TRUE, overflow_action = "error", - collect_overflows = FALSE + collect_overflows = FALSE, + na_string = "" ) } \arguments{ @@ -47,9 +48,10 @@ the row-overflow guard reports a single row whose committed height exceeds the available page content height (a row that wraps to taller than one page is almost always a sign of input that needs to change). The same knob downgrades column-overflow events; see \code{\link[=export_tfl_page]{export_tfl_page()}}. -The error has class \code{writetfl_error_row_too_tall} and the warning -\code{writetfl_warning_row_too_tall}; the message names the row, the column -holding its tallest cell, and the first 40 characters of that cell. +A row-too-tall error has class \code{writetfl_error_row_too_tall} and the +warning \code{writetfl_warning_row_too_tall}; the message names the row, the +column holding its tallest cell, and the first 40 characters of that cell +as the table shows it. Ignored when \code{collect_overflows = TRUE}.} \item{collect_overflows}{Logical. When \code{FALSE} (default) the function @@ -58,6 +60,9 @@ behaves as before: row-overflow events are routed through overflow but instead collects the events and returns them alongside the page specs, so a caller can iterate (see the row-overflow retry loop in \code{.tfl_table_to_pagelist_default()}).} + +\item{na_string}{Text shown for a missing cell, as in \code{tfl_table()}; used to +quote the cell in the row-too-tall message.} } \value{ When \code{collect_overflows = FALSE} (default), a list of row-page diff --git a/tests/testthat/test-tfl_table.R b/tests/testthat/test-tfl_table.R index 0bd67c7..a8312ca 100644 --- a/tests/testthat/test-tfl_table.R +++ b/tests/testthat/test-tfl_table.R @@ -1665,28 +1665,52 @@ test_that("tfl_table() rejects an NA column label up front", { }) # --------------------------------------------------------------------------- -# Row-overflow message names the column and the cell text (issue 44) +# Row-overflow message names the column and the cell text # --------------------------------------------------------------------------- test_that(".cell_text_prefix keeps up to 40 characters and cuts longer text with an ellipsis", { forty <- "0123456789012345678901234567890123456789" expect_equal(nchar(forty), 40L) - expect_equal(.cell_text_prefix(forty), forty) - expect_equal(.cell_text_prefix(paste0(forty, "x")), paste0(forty, "...")) - expect_equal(.cell_text_prefix(paste0(forty, "xyz and a lot more")), paste0(forty, "...")) - expect_equal(.cell_text_prefix("short"), "short") - expect_equal(.cell_text_prefix("", n = 5L), "") - expect_equal(.cell_text_prefix("abcdefgh", n = 5L), "abcde...") -}) - -test_that(".cell_text_prefix shows each line break as one space, and handles NA and list cells", { - expect_equal(.cell_text_prefix("one\ntwo\r\nthree\rfour"), "one two three four") - expect_equal(.cell_text_prefix("a\n\nb"), "a b") - expect_equal(.cell_text_prefix(NA_character_), "NA") - expect_equal(.cell_text_prefix(12.5), "12.5") - expect_equal(.cell_text_prefix(list(c("x", "y"))), "x y") - # The line break counts as a character, after it is made a space. - expect_equal(.cell_text_prefix("abc\ndef", n = 5L), "abc d...") + expect_equal(.cell_text_prefix(forty, ""), forty) + expect_equal(.cell_text_prefix(paste0(forty, "x"), ""), paste0(forty, "...")) + expect_equal(.cell_text_prefix(paste0(forty, "xyz and a lot more"), ""), paste0(forty, "...")) + expect_equal(.cell_text_prefix("short", ""), "short") + expect_equal(.cell_text_prefix("", "", n = 5L), "") + expect_equal(.cell_text_prefix("abcdefgh", "", n = 5L), "abcde...") +}) + +test_that(".cell_text_prefix counts characters, not bytes, for multibyte text", { + e_acute <- "é" + expect_equal(.cell_text_prefix(strrep(e_acute, 40L), ""), strrep(e_acute, 40L)) + expect_equal(.cell_text_prefix(strrep(e_acute, 41L), ""), paste0(strrep(e_acute, 40L), "...")) +}) + +test_that(".cell_text_prefix shows each line break as one space before it counts to the limit", { + expect_equal(.cell_text_prefix("one\ntwo\r\nthree\rfour", ""), "one two three four") + expect_equal(.cell_text_prefix("a\n\nb", ""), "a b") + expect_equal(.cell_text_prefix("\n\n", ""), " ") + expect_equal(.cell_text_prefix("abc\ndef", "", n = 5L), "abc d...") + # A CRLF straddling the 40-character limit is one space, so 39 characters + # plus the break plus one more is 41 characters long and is cut. + boundary <- paste0(strrep("a", 39L), "\r\nb") + expect_equal(.cell_text_prefix(boundary, ""), paste0(strrep("a", 39L), " ", "...")) + expect_equal(.cell_text_prefix(paste0(strrep("a", 39L), "\r\n"), ""), paste0(strrep("a", 39L), " ")) +}) + +test_that(".cell_text_prefix quotes what the table shows: na_string for NA, the level for a factor, as.character() for a list", { + expect_equal(.cell_text_prefix(NA_character_, "n/a"), "n/a") + expect_equal(.cell_text_prefix(NA_character_, ""), "") + expect_equal(.cell_text_prefix(NA_real_, "--"), "--") + expect_equal(.cell_text_prefix(12.5, ""), "12.5") + expect_equal(.cell_text_prefix(factor("level one"), ""), "level one") + expect_equal(.cell_text_prefix(list(c("x", "y")), ""), "c(\"x\", \"y\")") + expect_equal(.cell_text_prefix(list(NULL), ""), "NULL") + expect_equal(.cell_text_prefix(list(character(0)), ""), "character(0)") + # The cell is formatted by the same function the table is drawn with. + vals <- list(NA_character_, 3L, factor("f"), list(c("x", "y")), "text") + for (val in vals) { + expect_equal(.cell_text_prefix(val, "n/a"), .fmt_cell(val, "n/a")) + } }) test_that(".bottleneck_col picks the tallest wrap-eligible cell, else the tallest cell", { @@ -1696,21 +1720,31 @@ test_that(".bottleneck_col picks the tallest wrap-eligible cell, else the talles expect_equal(.bottleneck_col(c(1, 5, 3), c(FALSE, FALSE, FALSE)), 2L) # A wrap-eligible column with no height does not count. expect_equal(.bottleneck_col(c(1, 5, 0), c(FALSE, FALSE, TRUE)), 2L) + # A tie goes to the first column, wrap-eligible or not. + expect_equal(.bottleneck_col(c(5, 5, 1), c(TRUE, TRUE, TRUE)), 1L) + expect_equal(.bottleneck_col(c(5, 5, 1), c(FALSE, FALSE, FALSE)), 1L) + # Every cell has no height: the first column. + expect_equal(.bottleneck_col(c(0, 0, 0), c(TRUE, TRUE, TRUE)), 1L) + expect_equal(.bottleneck_col(c(0, 0, 0), c(FALSE, FALSE, FALSE)), 1L) }) -row_overflow_table <- function() { - essay <- paste( +row_overflow_essay <- function() { + paste( "The quick brown fox jumps over the lazy dog and keeps running.", paste(rep("aa bb cc dd ee ff", 360), collapse = " ") ) +} + +row_overflow_table <- function(...) { df <- data.frame( id = c("short row", "tall row"), - notes = c("fine", essay), + notes = c("fine", row_overflow_essay()), stringsAsFactors = FALSE ) tfl_table( df, - cols = list(tfl_colspec("notes", width = grid::unit(0.8, "inches"), wrap = TRUE)) + cols = list(tfl_colspec("notes", width = grid::unit(0.8, "inches"), wrap = TRUE)), + ... ) } @@ -1745,14 +1779,63 @@ test_that("a row too tall for the page warns with the same message and a warning expect_true(file.exists(f)) }) -test_that("paginate_rows names the tallest wrap-eligible column of the overflowing row", { - data <- data.frame(a = c("x", "AAAA"), b = c("y", "BBBB"), stringsAsFactors = FALSE) +test_that("the quoted cell text is the na_string the table shows for an NA cell", { + f <- tempfile(fileext = ".pdf") + on.exit(unlink(f)) + df <- data.frame(id = "only row", notes = NA_character_, stringsAsFactors = FALSE) + tbl <- tfl_table( + df, + cols = list(tfl_colspec("notes", width = grid::unit(0.8, "inches"), wrap = TRUE)), + na_string = row_overflow_essay() + ) + err <- expect_error( + export_tfl(tbl, file = f, pg_width = 4, pg_height = 8.5, + min_content_height = grid::unit(0.5, "inches")), + class = "writetfl_error_row_too_tall" + ) + expect_match( + conditionMessage(err), + "starts \"The quick brown fox jumps over the lazy ...\"", + fixed = TRUE + ) +}) + +test_that("column-width overflow errors and warnings carry no row-too-tall class", { + f <- tempfile(fileext = ".pdf") + on.exit(unlink(f)) + tbl <- tfl_table( + make_simple_df(), + cols = list( + tfl_colspec("label", width = grid::unit(4, "inches")), + tfl_colspec("value1", width = grid::unit(4, "inches")), + tfl_colspec("value2", width = grid::unit(4, "inches")) + ), + allow_col_split = FALSE + ) + err <- expect_error(export_tfl(tbl, file = f, pg_width = 8.5, pg_height = 11), + "exceeds available content width") + expect_false(inherits(err, "writetfl_error_row_too_tall")) + warn <- expect_warning( + export_tfl(make_wide_col_table(), file = f, pg_width = 8.5, pg_height = 11, + overflow_action = "warn"), + "Column 'a'" + ) + expect_false(inherits(warn, "writetfl_warning_row_too_tall")) +}) + +test_that("paginate_rows names the tallest wrap-eligible column of the overflowing row, not a taller column that cannot wrap", { + data <- data.frame( + a = c("x", "AAAA"), b = c("y", "BBBB"), c = c("z", "CCCC"), + stringsAsFactors = FALSE + ) resolved_cols <- list( list(col = "a", is_group_col = FALSE, wrap = TRUE), - list(col = "b", is_group_col = FALSE, wrap = TRUE) + list(col = "b", is_group_col = FALSE, wrap = TRUE), + list(col = "c", is_group_col = FALSE, wrap = FALSE) ) - # Row 2 is 6 + 9 inches tall; column b holds the tallest cell (9). - cell_h_mat <- rbind(c(0.2, 0.2), c(6, 9)) + # Row 2 is as tall as its tallest cell, 12 in (column c, which cannot + # wrap); column b (9 in) is the tallest wrap-eligible cell. + cell_h_mat <- rbind(c(0.2, 0.2, 0.2), c(6, 9, 12)) err <- expect_error( paginate_rows(data, cell_h_mat, resolved_cols, group_vars = character(0), cont_row_h = 0.2, header_row_h = 0.2, content_height_in = 3, @@ -1761,3 +1844,17 @@ test_that("paginate_rows names the tallest wrap-eligible column of the overflowi ) expect_match(conditionMessage(err), "in column 'b' and starts \"BBBB\"", fixed = TRUE) }) + +test_that("paginate_rows quotes the cell as the table shows it, with the na_string it is given", { + data <- data.frame(a = c("x", NA), stringsAsFactors = FALSE) + resolved_cols <- list(list(col = "a", is_group_col = FALSE, wrap = TRUE)) + cell_h_mat <- rbind(0.2, 9) + err <- expect_error( + paginate_rows(data, cell_h_mat, resolved_cols, group_vars = character(0), + cont_row_h = 0.2, header_row_h = 0.2, content_height_in = 3, + row_cont_msg = "(continued)", group_rule = FALSE, + na_string = "not available"), + class = "writetfl_error_row_too_tall" + ) + expect_match(conditionMessage(err), "starts \"not available\"", fixed = TRUE) +}) diff --git a/vignettes/v04-troubleshooting.Rmd b/vignettes/v04-troubleshooting.Rmd index d392380..119dc1f 100644 --- a/vignettes/v04-troubleshooting.Rmd +++ b/vignettes/v04-troubleshooting.Rmd @@ -104,7 +104,13 @@ you see what the renderer was forced to clip. ## Row wrapped to taller than one page -**Error:** `Row N of the table wraps to a height (X.XX in) that exceeds the available page content height (Y.YY in)…` +**Error:** `Row N of the table wraps to a height (X.XX in) that exceeds the available page content height (Y.YY in). The tallest cell is in column 'name' and starts "…"…` + +The message names the row, the column holding its tallest cell, and the first +40 characters of that cell as the table shows it (line breaks as spaces, `...` +after a longer text), so the row can be found by searching the data. The error +has class `writetfl_error_row_too_tall`; under `overflow_action = "warn"` the +warning has class `writetfl_warning_row_too_tall`. A row's content wrapped to more vertical space than fits on a single page. Under the default `col_split_strategy = "balanced"` the package @@ -135,9 +141,10 @@ where the first row-overflow signals immediately), set ```r export_tfl(tbl, file = "out.pdf", overflow_action = "warn") #> Warning: Row 3 of the table wraps to a height (12.4 in) that exceeds -#> the available page content height (7.3 in). Reduce the cell content, -#> increase the page height, widen the column, or set the column to -#> wrap less aggressively. +#> the available page content height (7.3 in). The tallest cell is in +#> column 'notes' and starts "The quick brown fox jumps over the lazy ...". +#> Reduce the cell content, increase the page height, widen the column, +#> or set the column to wrap less aggressively. ``` ## Overlap warnings