Export a list of TFLs to a multi-page PDF
export_tfl.RdOpens a PDF device, renders each page using export_tfl_page(), and
closes the device. Guarantees device closure via on.exit() even if an
error occurs during rendering.
When preview is not FALSE, no PDF is written. Instead the selected pages
are drawn to the currently open graphics device (useful in RStudio, Positron,
or knitr chunks) and returned as a list of grid grobs.
Usage
export_tfl(
x,
file = NULL,
pg_width = 11,
pg_height = 8.5,
page_num = "Page {i} of {n}",
preview = FALSE,
...
)Arguments
- x
A single
ggplotobject, a grid grob (e.g. fromgt::as_gtable()orgridExtra::tableGrob()), atfl_table()object, aggtibbleobject (from the ggtibble package), agt_tblobject (from the gt package), a list ofgt_tblobjects, or a named list of page specifications. Each page specification is a list with a requiredcontentelement (aggplotor grob) and optional elements corresponding to the text arguments ofexport_tfl_page():header_left,header_center,header_right,caption,footnote,footer_left,footer_center,footer_right. Per-page list elements take precedence over values supplied via....When
xis atfl_table()object, pagination and grob construction are performed automatically. Page layout arguments (pg_width,pg_height, and any arguments in...such asmargins,padding, and annotations) are used both to compute available space and to render each page.When
xis aggtibbleobject, each row becomes a page. Thefigurecolumn provides the content; any columns whose names matchexport_tfl_page()text arguments (caption,footnote,header_left, etc.) are used as per-page values. Other columns are ignored.When
xis agt_tblobject, the title and subtitle are extracted as the caption, source notes and footnotes are extracted as the footnote, and the table body is rendered as a grid grob viagt::as_gtable(). A list ofgt_tblobjects produces one page (or more, with pagination) per table.When
xis aVTableTreeobject (from the rtables package), the main title and subtitles are extracted as the caption, and main footer and provenance footer are extracted as the footnote. The table is rendered as monospace text viatoString()and wrapped in a gridtextGrob. Pagination uses rtables' built-inpaginate_table(). A list ofVTableTreeobjects produces one page (or more, with pagination) per table.When
xis aflextableobject (from the flextable package), the caption (fromflextable::set_caption()) is extracted as the caption, and footer rows (fromflextable::footnote()orflextable::add_footer_lines()) are extracted as the footnote. The table is rendered viaflextable::gen_grob(). A list offlextableobjects produces one page (or more, with pagination) per table.When
xis atable1object (from the table1 package), the caption and footnote are extracted from the table1 object's internal structure. The table is converted to a flextable viatable1::t1flex(), preserving column labels, bold variable names, and indented summary statistics. Pagination is group-aware: page breaks fall between variable groups (label + summary rows) rather than splitting a group mid-way. A list oftable1objects produces one page (or more, with pagination) per table.- file
Path to the output PDF file. Must be a single character string ending in
".pdf". Not required whenpreviewis notFALSE.- pg_width
Page width in inches.
- pg_height
Page height in inches.
- page_num
A
glue::glue()specification for automatic page numbering, where{i}is the current page number and{n}is the total number of pages. Set toNULLto disable.- preview
Controls preview rendering instead of PDF output:
FALSE(default): write tofileas normal.TRUE: render all pages to the current graphics device.An integer vector: render only the specified page numbers (e.g.
preview = c(1, 3)renders pages 1 and 3).
In preview mode each page is drawn via
grid::grid.newpage()(so knitr captures it as an inline graphic). ReturnsNULLinvisibly.- ...
Arguments passed on to
export_tfl_pagepaddingVertical space between adjacent present sections, as a
unitobject. Separator rules (if enabled) are drawn at the midpoint of this gap and do not consume additional space.header_left,header_center,header_rightHeader text. Accepts
NULL, a single string, or a character vector (collapsed with"\\n"). Horizontal justification follows the argument name (left/center/right). Vertically top-justified. Overridden byx$header_leftetc.captionCaption text below the header and above the content. Accepts
NULL, a single string, or a character vector. Full-width; justification controlled bycaption_just. Overridden byx$caption.footnoteFootnote text below the content. Accepts
NULL, a single string, or a character vector. Full-width; justification controlled byfootnote_just. Overridden byx$footnote.footer_left,footer_center,footer_rightFooter text. Mirror of header arguments. Vertically bottom-justified. Overridden by
x$footer_leftetc.gpTypography specification. Accepts either a single
gpar()object applied to all text, or a named list for section- or element-level control. Resolution priority (highest first): element-level (e.g.gp$header_left), section-level (e.g.gp$header), globalgpar(). Example:header_ruleSeparator rule drawn between the header and the next section (caption or content), fitted within the
paddinggap. Accepts:FALSE: no ruleTRUE: full-width ruleA numeric in
(0, 1]: rule spanning that fraction of viewport width, centeredA grob (typically a
linesGrob): drawn as-is, centered vertically in the padding gap.
footer_ruleSeparator rule between the last body section (footnote or content) and the footer. Same specification as
header_rule.caption_justHorizontal justification for the caption.
footnote_justHorizontal justification for the footnote.
content_justHorizontal justification for character string content. One of
"left"(default),"right", or"centre". Ignored whenx$contentis a ggplot or grob.marginsOuter page margins as a
unitvector with elementst,r,b,l(top, right, bottom, left).min_content_heightMinimum acceptable content area height as a
unitobject. An error is raised if the computed content height falls below this value.overflow_actionOne of
"error"(default) or"warn". Controls how width-overflow conditions are reported when the content does not fit in its allocated area:"error": append the message to the layout-error vector and abort before drawing (no PDF page is produced)."warn": emitrlang::warn()and continue rendering. The PDF is produced with the overflow visibly clipped bygrid, which is useful for diagnosing what is too wide. See issue #30.
The same setting applies to all width-overflow detections: the page-level content grob check (any
grobcontent wider than the content viewport), thetfl_table()total-width check (whenallow_col_split = FALSEand the column total still exceeds the page after wrapping), and thetfl_table()per-column check (any single column — or any data column combined with the row-header group columns — wider than the page).
Value
Normal mode (
preview = FALSE): the normalized absolute path to the PDF file, returned invisibly.Preview mode:
NULL, invisibly.
Details
Arguments forwarded via ... serve as defaults for all pages and are
overridden by per-page list elements in x.
See also
export_tfl_page() for single-page layout control.
Examples
if (FALSE) { # \dontrun{
library(ggplot2)
# Single plot
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
export_tfl(p, "single.pdf")
# Multiple plots with per-page captions
plots <- list(
list(content = p, caption = "Weight vs MPG"),
list(content = ggplot(mtcars, aes(hp, mpg)) + geom_point(),
caption = "Horsepower vs MPG")
)
export_tfl(plots, "report.pdf",
header_left = "My Report",
header_right = format(Sys.Date())
)
# Preview the first two pages without writing a file
export_tfl(plots, preview = c(1, 2),
header_left = "My Report"
)
} # }