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.- ...
Additional arguments passed to
export_tfl_page(). These serve as defaults for all pages and are overridden by per-page list elements inx.
Value
Normal mode (
preview = FALSE): the normalized absolute path to the PDF file, returned invisibly.Preview mode:
NULL, invisibly.
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"
)
} # }