Define a data frame table for PDF export
tfl_table.RdCreates a table configuration object that can be passed to export_tfl().
All measurement, pagination, and rendering are deferred until export_tfl()
is called, at which point page layout information (dimensions, margins,
annotations) is available.
Column properties can be specified via tfl_colspec() objects in cols,
via the flat arguments (col_widths, col_labels, etc.), or both. When
both name the same column, tfl_colspec() takes priority.
Row-header columns (which repeat on every column-split page) are detected
automatically from dplyr::group_vars(x). Group columns must appear
as the first columns of x in the same order as dplyr::group_vars(x);
an error is raised if not.
Usage
tfl_table(
x,
cols = NULL,
col_widths = NULL,
col_labels = NULL,
col_align = NULL,
wrap_cols = FALSE,
min_col_width = grid::unit(0.5, "inches"),
allow_col_split = TRUE,
balance_col_pages = FALSE,
suppress_repeated_groups = TRUE,
col_cont_msg = "Columns continue on other pages",
row_cont_msg = c("(continued)", "(continued on next page)"),
show_col_names = TRUE,
col_header_rule = TRUE,
group_rule = TRUE,
group_rule_after_last = FALSE,
row_rule = FALSE,
row_header_sep = FALSE,
fill_by = "row",
na_string = "",
gp = list(),
cell_padding = grid::unit(c(0.2, 0.5), "lines"),
line_height = 1.05,
max_measure_rows = Inf
)Arguments
- x
A data frame or grouped tibble (from
dplyr::group_by()).- cols
A list of
tfl_colspec()objects, orNULLto auto-specify all columns. Whencolsis provided it may be partial — columns not named incolsfall back to flat arguments or type-based defaults. Column display order always follows the source data frame, regardless of the order of entries incols.- col_widths
Named vector: each element is either a
unitobject (fixed width) or a plain positive numeric (relative weight). Names must match column names inx. Overridden per-column bytfl_colspec(width).- col_labels
Named character vector of display labels. Use
"\n"for multiline column headers. Overridden per-column bytfl_colspec(label).- col_align
Named character vector. Each element is
"left","right", or"centre". Overridden per-column bytfl_colspec(align).- wrap_cols
Column-wrapping eligibility.
TRUE= all non-group columns eligible;FALSE= none eligible; character vector = those specific column names. Overridden per-column bytfl_colspec(wrap).- min_col_width
Minimum column width as a
unitobject.- allow_col_split
Logical. If
FALSE, an error is raised when total column width still exceeds available width after wrapping. IfTRUE(default), columns are split across pages.- balance_col_pages
Logical. When
TRUEand column pagination produces more than one page, the data columns are redistributed across the pages so that each page receives approximately the same number of columns. The greedy pass is still used to determine the minimum number of pages required, and each balanced group is verified to fit; if a balanced group would overflow the available width the greedy layout is used as a fallback. DefaultFALSE.- suppress_repeated_groups
Logical. When
TRUE(default), group column cells whose value equals the immediately preceding rendered row on the same page are left blank. The first data row on each page always shows the group value.- col_cont_msg
Character scalar or
NULL. Message displayed as rotated side labels on column-split pages: clockwise 90° to the right of the table when columns continue on a subsequent page, and counter-clockwise 90° to the left of the table (including row-label columns) when columns continue from a prior page. Set toNULLto disable.- row_cont_msg
Character vector of length 1 or 2. The first element is shown at the top of a continuation page; the second is shown at the bottom of the preceding page. A length-1 value is recycled to both positions. Default:
c("(continued)", "(continued on next page)").- show_col_names
Logical. If
FALSE, the column header row is omitted andcol_header_ruleis also suppressed.- col_header_rule
Logical. If
TRUE(default), a horizontal rule is drawn below the column header row.- group_rule
Logical. If
TRUE(default), a horizontal rule is drawn between row groups.- group_rule_after_last
Logical. If
TRUE, a rule is also drawn after the last group on a page. DefaultFALSE.- row_rule
Logical. If
TRUE, a horizontal rule is drawn between every data row. Style is controlled viagp$row_rule. DefaultFALSE.- row_header_sep
Logical. If
TRUE, a vertical rule is drawn at the right edge of the last row-header column, spanning data rows only (not the column header row). DefaultFALSE.- fill_by
Character scalar controlling how
gp$data_row$fillcolor vectors are cycled."row"(default) advances the color index for every data row."group"advances only at group boundaries, so all rows in the same group share one fill color.- na_string
Character scalar. Replacement text for
NAvalues. Default"".- gp
A named list of
gpar()objects controlling table-internal typography and rule styles. Page-annotation typography is controlled separately via thegpargument ofexport_tfl_page(). Recognised keys:gp$tableBase font for all table text.
gp$header_rowColumn header row. Default: bold. Set
fillfor a background color (e.g.,gpar(fontface = "bold", fill = "lightblue")).gp$data_rowData cell text. Inherits
gp$table. Setfillfor background color; use a vector for alternating rows or groups (e.g.,gpar(fill = c("white", "gray95"))). Seefill_by.gp$group_colRow-header column cells. Inherits
gp$table.gp$continuedContinuation-marker row text. Default: italic.
gp$col_header_ruleStyle of the column-header rule.
gp$group_ruleStyle of between-group rules.
gp$row_ruleStyle of between-row data rules.
gp$row_header_sepStyle of the vertical row-header separator.
- cell_padding
Padding inside each cell. Accepts a
unitof length:1: applied to all four sides
2:
c(vertical, horizontal)— first element for top/bottom, second for left/right4:
c(top, right, bottom, left)— CSS-style per-side control
Example:
unit(c(0.2, 0.5), "lines")for 0.2 lines vertical, 0.5 lines horizontal. Note: named vectors are not supported becausegrid::unit()does not preserve names from numeric vectors.- line_height
A positive numeric multiplier that controls the spacing between lines within a multi-line (word-wrapped) cell. A value of
1.0packs lines baseline-to-baseline with no extra gap; the default1.05adds a small 5% breathing room. If agpar()supplied through thegpargument already contains an explicitlineheightfield for a particular section, that value takes precedence over this parameter.- max_measure_rows
Positive numeric or
Inf(default). Maximum number of unique cell strings sampled per column when computing content-based column widths. Strings are sampled in descending order ofnchar()so the widest strings are always measured. Also limits the number of data rows sampled for row-height estimation.
Value
An object of class "tfl_table". Pass directly to export_tfl().
Examples
if (FALSE) { # \dontrun{
library(dplyr)
df <- group_by(mtcars, cyl)
tbl <- tfl_table(
df,
col_labels = c(mpg = "MPG", hp = "Horse-\npower"),
col_align = c(mpg = "right", hp = "right"),
wrap_cols = FALSE
)
export_tfl(tbl,
file = "cars.pdf",
header_left = "Study XYZ",
caption = "Table 1. Motor Trend Cars",
page_num = "Page {i} of {n}")
} # }