Publication requires tables in specific formats: PDF for static
outputs, Word documents for collaborative drafting, LaTeX for manuscript
preparation, HTML for web supplements, PowerPoint for presentations, and
RTF for broad compatability. The summata package provides
export functions for each format, with consistent parameters across
formats and format-specific options where needed.
| Function | Format | Dependencies |
|---|---|---|
tablesave() |
Any of the below, based on file extension | Varies |
data.table::fwrite() |
CSV / TSV | data.table |
table2pdf() |
xtable, LaTeX distribution |
|
table2docx() |
Microsoft Word (.docx) | flextable, officer |
table2html() |
HTML | xtable |
table2pptx() |
PowerPoint (.pptx) | flextable, officer |
table2tex() |
LaTeX source (.tex) | xtable |
table2rtf() |
Rich Text Format (.rtf) | flextable |
tablesave() writes any of these formats, choosing one
from the file extension. The format-specific functions may also be
called directly if preferred. The syntax for all functions follows the
same paradigm:
where data is the dataset, and "output.pdf"
is the name of the destination file. This vignette demonstrates the
various capabilities of these functions using the included sample
dataset.
The examples in this vignette create sample tables using
summata functions:
library(summata)
data(clintrial)
data(clintrial_labels)
# Create sample tables for export
table1 <- desctable(
data = clintrial,
by = "treatment",
variables = c("age", "sex", "bmi", "smoking", "stage", "ecog"),
labels = clintrial_labels
)
table2 <- fullfit(
data = clintrial,
outcome = "surgery",
predictors = c("age", "sex", "stage", "treatment", "ecog"),
model_type = "glm",
labels = clintrial_labels
)
table3 <- fullfit(
data = clintrial,
outcome = "Surv(os_months, os_status)",
predictors = c("age", "sex", "stage", "treatment", "ecog"),
model_type = "coxph",
labels = clintrial_labels
)
table4 <- compfit(
data = clintrial,
outcome = "surgery",
model_list = list(
"Demographics" = c("age", "sex"),
"Clinical" = c("age", "sex", "stage", "ecog"),
"Full" = c("age", "sex", "stage", "ecog", "treatment")
),
model_type = "glm"
)Delimited text is the simplest export path and requires no
dependencies beyond data.table, which summata
already imports. Since all summata output tables are
data.table objects, data.table::fwrite() may
be used directly.
Standard CSV export can be performed using
data.table::fwrite():
TSV export can be performed by using
data.table::fwrite() with a specified tab delimiter:
Like data.table::fwrite(), tablesave() also
accepts a custom delimiter:
CSV and TSV files are useful for archiving raw table output, importing into spreadsheet software, or downstream processing in other statistical environments. For formatted, publication-ready output, use the export functions described in the sections that follow.
The table2pdf() function creates PDF documents using
LaTeX typesetting via the xtable package. A LaTeX
distribution (TinyTeX, TeX Live, MiKTeX, or MacTeX) is required.
The simplest usage requires only a table and filename:
Add a caption using the caption parameter:
Multiple formatting options can be combined:
Control page size and orientation:
Adjust page margins (in inches):
Scale tables to fit the page width:
The table2docx() function creates editable Word
documents using the flextable and officer
packages.
The simplest usage requires only a table and filename:
Add a caption using the caption parameter:
Multiple formatting options can be combined:
Control page size and orientation for wide tables:
The table2html() function exports tables to HTML file
output for web use via the xtable package.
The simplest usage requires only a table and filename:
Add a caption using the caption parameter:
The table2pptx() function creates PowerPoint
presentations with tables using the flextable and
officer packages.
The simplest usage creates a single-slide presentation:
Add a slide title using the slide_title parameter:
Multiple formatting options can be combined:
The table2tex() function creates LaTeX source files for
inclusion in manuscripts via the xtable package.
The simplest usage requires only a table and filename:
Add caption and cross-reference label:
The table2rtf() function creates Rich Text Format files
using the flextable and officer packages.
The simplest usage requires only a table and filename:
Multiple formatting options can be combined:
The tablesave() function writes a table in the format
indicated by the file extension, simplifying the export workflow:
tablesave(table1, "table1.csv") # CSV output
tablesave(table1, "table1.tsv") # TSV output
tablesave(table1, "table1.ssv", sep = " ") # SSV output
tablesave(table1, "table1.pdf") # PDF output
tablesave(table1, "table1.docx") # DOCX output
tablesave(table1, "table1.html") # HTML output
tablesave(table1, "table1.pptx") # PPTX output
tablesave(table1, "table1.tex") # TeX output
tablesave(table1, "table1.rtf") # RTF outputFormat-specific parameters pass through to the underlying function:
tablesave(
table = table2,
file = "table2.pdf",
caption = "Table 2. Regression Analysis",
orientation = "landscape",
font_size = 8,
bold_significant = TRUE,
indent_groups = TRUE,
zebra_stripes = TRUE
)All export functions share common parameters for consistent results:
| Parameter | Description | Default |
|---|---|---|
caption |
Table caption/title | NULL |
font_size |
Base font size (points) | 8–10 |
format_headers |
Auto-format column headers | TRUE |
bold_significant |
Bold significant p-values | TRUE |
p_threshold |
Significance threshold | 0.05 |
indent_groups |
Indent factor levels | FALSE |
condense_table |
Show essential rows only | FALSE |
condense_quantitative |
Condense continuous/survival only
(desctable() only) |
FALSE |
zebra_stripes |
Alternating row shading | FALSE |
dark_header |
Dark header background | FALSE |
The condense_table and
condense_quantitative parameters reduce table height:
condense_table = TRUE: Condenses continuous, survival,
and binary categorical variablescondense_quantitative = TRUE: Condenses only continuous
and survival variables (desctable() outputs only)# Full display
table2docx(table1, "table1_full.docx")
# Condense all variable types
table2docx(
table = table1,
file = "table1_condensed.docx",
condense_table = TRUE,
zebra_stripes = TRUE
)
# Condense only continuous/survival (descriptive tables only)
table2docx(
table = table1,
file = "table1_condensequant.docx",
condense_quantitative = TRUE,
zebra_stripes = TRUE
)| Parameter | Description | Default |
|---|---|---|
margins |
Page margins
c(top, right, bottom, left) |
c(1, 1, 1, 1) |
fit_to_page |
Scale table to fit page width | TRUE |
cell_padding |
Vertical padding (“none”, “normal”, “relaxed”, “loose”) | "normal" |
variable_padding |
Add spacing between variables | FALSE |
show_logs |
Keep LaTeX log files | FALSE |
| Parameter | Description | Default |
|---|---|---|
font_family |
Font name | Arial |
width |
Table width (inches) | NULL |
align |
Column alignment vector | NULL |
return_ft |
Return flextable object | FALSE |
| Parameter | Description | Default |
|---|---|---|
include_css |
Include embedded CSS styling | FALSE |
stripe_color |
Zebra stripe color (hex or name) | "#EEEEEE" |
| Parameter | Description | Default |
|---|---|---|
booktabs |
Use booktabs package styling | FALSE |
label |
LaTeX cross-reference label | NULL |
cell_padding |
Arraystretch value | "normal" |
| Parameter | Description | Default |
|---|---|---|
template |
Custom PPTX template file | NULL |
layout |
Slide layout name | "Title and Content" |
master |
Slide master name | "Office Theme" |
left, top |
Table position (inches) | 0.5, 1.5 |
| Use-Case | Recommended Format |
|---|---|
| Data archival, further processing | CSV (.csv) or TSV (.tsv) |
| Journal submission | PDF (.pdf) or LaTeX
(.tex) |
| Manuscript drafts, collaboration | Word (.docx) |
| Web/R Markdown | HTML (.html) |
| Presentations | PowerPoint (.pptx) |
| Maximum compatibility | RTF (.rtf) |
paper = "auto" for
embeddingExport to multiple formats with consistent formatting:
common_opts <- list(
caption = "Table 2. Regression Analysis",
bold_significant = TRUE,
indent_groups = TRUE,
zebra_stripes = TRUE
)
formats <- c("csv", "pdf", "docx", "html", "pptx", "rtf", "tex")
for (fmt in formats) {
tablesave(
table = table2,
file = paste0("Table2.", fmt),
caption = common_opts$caption,
bold_significant = common_opts$bold_significant,
indent_groups = common_opts$indent_groups,
zebra_stripes = common_opts$zebra_stripes
)
}Verify LaTeX installation and consider keeping log files for debugging:
# Check LaTeX installation
Sys.which("pdflatex")
# Install TinyTeX if needed
# tinytex::install_tinytex()
# Keep log files for debugging
table2pdf(table, "debug.pdf", show_logs = TRUE)See Installation and Setup for LaTeX configuration details.
Several options address wide tables:
desctable() for baseline characteristicsuniscreen(), fit(), and
fullfit()compfit() for comparing modelsmultifit() for multi-outcome analysis