Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions R/layout.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion R/table_pagelist.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 39 additions & 11 deletions R/table_rows.R
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,23 @@ 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, 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
}

# 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
Expand Down Expand Up @@ -246,13 +263,19 @@ 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()].
#' 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
#' `overflow_action`. When `TRUE` the function does *not* signal on
#' 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
Expand All @@ -267,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
Expand Down Expand Up @@ -345,28 +369,32 @@ 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 {
# 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 ",
"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], na_string)
)
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.
Expand All @@ -387,7 +415,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
Expand Down
3 changes: 3 additions & 0 deletions design/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```

---
Expand Down
30 changes: 30 additions & 0 deletions design/DECISIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
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).
10 changes: 9 additions & 1 deletion man/paginate_rows.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading