Deparse a table's column header back to editable rtf_col_header() source
Source: R/header_source.R
rtf_header_source.RdRenders the current column header of a finished rtftable() (or the
first page of an as_rtftables() list) as rtf_col_header(...) source
text, addressed by column name, so you can copy it, edit the labels /
spans, and re-apply with set_col_header(). It is the inverse companion of
set_col_header() and pairs with rtf_columns() (which lists the final
column names).
Usage
rtf_header_source(
x,
level = c("explicit", "default", "all"),
snippet = TRUE,
add_span_level = FALSE,
stub = 1
)Arguments
- x
An
rtftable(), or a list of them (pages fromas_rtftables(); the first page is used).- level
Verbosity of the emitted cells:
"explicit"(default – only fields that differ from the defaults),"default"(also show each cell's effectivealignand default border widths, but not theFALSEdecoration flags), or"all"(everything, includingbold = FALSEetc.).- snippet
Logical (default
TRUE). WhenTRUE, wrap the header in a pipeableset_col_header(...)call that also reproduces the header text alignment (fromcol_spec) and the header-related zone borders (header/spanning, viastyle_zone()). There is no leadingtbl |>– the first argument is meant to arrive through the pipe (e.g.my_tbl |>followed by the snippet). WhenFALSE, return the barertf_col_header(...)value.- add_span_level
Logical (default
FALSE). WhenTRUE, prepend a scaffold spanning row grouping the non-stubcolumns (see Details).- stub
Column(s) to keep un-spanned when
add_span_level = TRUE: integer position(s) and/or column name(s). Default1(the first column, whereas_rtftables(stub = )places the stub).
Value
A single character string (the source). Use cat() to print it
with the line breaks rendered.
Details
The output is name-based (cell positions are written as column names, which survive reordering), keeps the header's empty gap cells, and backtick-quotes non-syntactic column names. Per-cell text decorations and borders are reproduced faithfully.
add_span_level = TRUE previews adding a second hierarchy level: the
stub column(s) stay as single (empty) cells and every other column is
bundled under one empty spanning cell whose label you fill in – a quick
scaffold for turning a one-row header into a grouped, two-row header.
See also
set_col_header() to apply an edited header; rtf_columns() for
the final column names; rtf_col_header() / col_cell() for the pieces.
Examples
tbl <- rtftable(
data.frame(row_label = "x", g1 = 1, g2 = 2, Total = 3),
col_header = c("Category", "Low", "High", "Total")
)
cat(rtf_header_source(tbl, snippet = FALSE))
#> rtf_col_header(
#> c(row_label = "Category", g1 = "Low", g2 = "High", Total = "Total")
#> )
# Preview adding a spanning level over the non-stub columns:
cat(rtf_header_source(tbl, snippet = FALSE, add_span_level = TRUE))
#> rtf_col_header(
#> list(col_cell("row_label", ""), col_cell(c("g1", "Total"), "")),
#> c(row_label = "Category", g1 = "Low", g2 = "High", Total = "Total")
#> )