Checks a configured fr_spec for common misconfigurations that would cause
errors or unexpected output at render time. Returns the spec invisibly when
valid, making it pipeline-friendly. In strict mode, any issue raises an
error.
Arguments
- spec
An
fr_specobject fromfr_table().- strict
Logical. If
TRUE, any validation issue raises an error. IfFALSE(default), issues are reported as warnings and the spec is returned invisibly.
Value
Invisibly returns spec when valid (or when strict = FALSE).
Raises an error in strict mode when issues are found.
Checks performed
Column names in
fr_cols()exist in datapage_by,group_by,indent_by,blank_aftercolumns existstub_colsexist in dataN-count column names match column specs
Span columns exist and are contiguous
Column widths sum to printable area (warning if >110%)
Style row/col indices are in valid range
Font family is recognised
sort_by/repeat_colscolumns exist (for listings)
Examples
## ── Clean spec passes validation silently ──────────────────────────────────
spec <- tbl_demog |>
fr_table() |>
fr_hlines("header")
spec |> fr_validate()
## ── Strict mode on a clean spec (no error) ────────────────────────────────
spec |> fr_validate(strict = TRUE)
## ── Spec with a validation issue: bad column in column specs ────────────────
# Manually inject a column spec for a non-existent column
# (fr_cols() validates eagerly, so we set it directly for demo)
bad_spec <- tbl_demog |> fr_table()
bad_spec$columns[["nonexistent"]] <- fr_col("Bad Column")
# Non-strict mode: warns but returns the spec
bad_spec |> fr_validate()
#> Warning: ! 1 validation issue found:
#> • Column in `fr_cols()` not found in data: "nonexistent".
# Strict mode errors (wrapped in tryCatch for safety)
tryCatch(
bad_spec |> fr_validate(strict = TRUE),
error = function(e) message("Caught: ", conditionMessage(e))
)
#> Caught: ! 1 validation issue found:
#> • Column in `fr_cols()` not found in data: "nonexistent".
## ── Validate before rendering in a pipeline ───────────────────────────────
out <- file.path(tempdir(), "validated.rtf")
tbl_ae_soc |>
fr_table() |>
fr_titles("Table 14.3.1", "AE by SOC") |>
fr_hlines("header") |>
fr_validate() |>
fr_render(out)
unlink(out)