tablespan provides a unified, formula-driven approach to creating complex tables with multi-level headers (spanners) that can be exported to a variety of output formats. The core idea is to define the structure of a table once, using a concise formula syntax, and then reuse that structure across multiple reporting outputs such as Excel, HTML, LaTeX, Word, Typst, or PowerPoint.
Exporting is a core design goal of
tablespan: instead of manually recreating tables for
different formats, you can generate a single Tablespan
object and convert it into the format that best fits your reporting
workflow.
There are two primary motivations for using tablespan:
Easily create “good enough” tables that can be exported
to many formats. tablespan is designed to quickly
generate basic tables that work across a wide variety of outputs. This
is ideal for internal or semi-formal reporting where the same
table needs to be delivered as:
Easily build a good starting point table that can then be
refined with more sophisticated packages (e.g., gt or
flextable). tablespan provides a concise syntax
for relatively basic tables with title, subtitle, header, row names,
columns and footnotes. Once exported to one of the supported packages
(e.g., gt), these tables can easily be refined to be publication
ready.
library(tablespan)
library(dplyr)
data(mtcars)
summarized_table <- mtcars |>
group_by(cyl, vs) |>
summarise(
N = n(),
mean_hp = mean(hp),
sd_hp = sd(hp),
mean_wt = mean(wt),
sd_wt = sd(wt),
.groups = "drop")
tbl <- tablespan(
data = summarized_table,
formula = Cylinder:cyl + Engine:vs ~
N +
(`Horse Power` = Mean:mean_hp + SD:sd_hp) +
(`Weight` = Mean:mean_wt + SD:sd_wt),
title = "Motor Trend Car Road Tests",
subtitle = "A table created with tablespan",
footnote = "Data from the mtcars data set")At this point, tbl is a Tablespan object
that contains the complete logical definition of the table and can be
exported or further styled.
Before exporting, basic styling can be applied directly at the tablespan level.
For example, we can apply a color scale to the mean_hp
column:
tbl <- tbl |>
style_column(
columns = mean_hp,
text_color = "#FFFFFF",
color_scale = c("#01016f" = NA, "#d8031c" = NA))Any style applied directly with tablespan will be automatically exported to all other table formats (e.g., gt).
Before exporting, keep the following points in mind:
The required backend packages must be installed tablespan delegates exporting to other packages. If a backend package is not installed, the corresponding export will not work (e.g., make sure that gt is installed to export to gt).
flextable and huxtable must be loaded For
as_flextable() and as_huxtable() to work, the
respective packages must be loaded.
To use the excel export, please make sure that openxlsx is installed.
.xlsx“The
gtpackage in R is a powerful tool for creating elegant and customizable tables for data visualization and reporting. It offers a user-friendly way to design and style tables in RMarkdown documents and Shiny applications.”
To use the gt export, please make sure that gt is installed.
Supported export formats:
.docx)| Motor Trend Car Road Tests | ||||||
| A table created with tablespan | ||||||
| Cylinder | Engine | N |
Horse Power
|
Weight
|
||
|---|---|---|---|---|---|---|
| Mean | SD | Mean | SD | |||
| 4 | 0 | 1 | 91.00 | 2.1400 | ||
| 4 | 1 | 10 | 81.80 | 21.872 | 2.3003 | 0.60 |
| 6 | 0 | 3 | 131.67 | 37.528 | 2.7550 | 0.13 |
| 6 | 1 | 4 | 115.25 | 9.179 | 3.3887 | 0.12 |
| 8 | 0 | 14 | 209.21 | 50.977 | 3.9992 | 0.76 |
| Data from the mtcars data set | ||||||
Optionally adapt the table with styles not supported directly within
tablespan:
| Motor Trend Car Road Tests | ||||||
| A table created with tablespan | ||||||
| Cylinder | Engine | N |
Horse Power
|
Weight
|
||
|---|---|---|---|---|---|---|
| Mean | SD | Mean | SD | |||
| 4 | 0 | 1 | 91.00 | 2.1400 | ||
| 4 | 1 | 10 | 81.80 | 21.872 | 2.3003 | 0.60 |
| 6 | 0 | 3 | 131.67 | 37.528 | 2.7550 | 0.13 |
| 6 | 1 | 4 | 115.25 | 9.179 | 3.3887 | 0.12 |
| 8 | 0 | 14 | 209.21 | 50.977 | 3.9992 | 0.76 |
| Data from the mtcars data set | ||||||
“The flextable package provides a framework to easily create tables for reporting and publications. Functions are provided to let users create tables, modify and format their content, and define their content.”
To use the flextabe export, please make sure that flextable is installed and loaded.
.docx).pptx)Motor Trend Car Road Tests | ||||||
|---|---|---|---|---|---|---|
A table created with tablespan | ||||||
Horse Power | Weight | |||||
Cylinder | Engine | N | Mean | SD | Mean | SD |
4 | 0 | 1 | 91.00 | 2.1400 | ||
4 | 1 | 10 | 81.80 | 21.872 | 2.3003 | 0.60 |
6 | 0 | 3 | 131.67 | 37.528 | 2.7550 | 0.13 |
6 | 1 | 4 | 115.25 | 9.179 | 3.3887 | 0.12 |
8 | 0 | 14 | 209.21 | 50.977 | 3.9992 | 0.76 |
Data from the mtcars data set | ||||||
Optionally post-process the table:
compose(ft,
j = 5,
value = flextable::as_paragraph(
flextable::minibar(value = sd_hp,
max = max(sd_hp, na.rm = TRUE))),
part = "body")Motor Trend Car Road Tests | ||||||
|---|---|---|---|---|---|---|
A table created with tablespan | ||||||
Horse Power | Weight | |||||
Cylinder | Engine | N | Mean | SD | Mean | SD |
4 | 0 | 1 | 91.00 | 2.1400 | ||
4 | 1 | 10 | 81.80 | 2.3003 | 0.60 | |
6 | 0 | 3 | 131.67 | 2.7550 | 0.13 | |
6 | 1 | 4 | 115.25 | 3.3887 | 0.12 | |
8 | 0 | 14 | 209.21 | 3.9992 | 0.76 | |
Data from the mtcars data set | ||||||
“Huxtable is an R package to create LaTeX and HTML tables, with a friendly, modern interface. Features include control over text styling, number format, background color, borders, padding and alignment. Cells can span multiple rows and/or columns. Tables can be manipulated with standard R subsetting or
dplyrfunctions.”
To use the huxtable export, please make sure that huxtable is installed and loaded.
.xlsx).docx)| Motor Trend Car Road Tests | ||||||
| A table created with tablespan | ||||||
| Horse Power | Weight | |||||
| Cylinder | Engine | N | Mean | SD | Mean | SD |
| 4 | 0 | 1 | 91.00 | 2.1400 | ||
| 4 | 1 | 10 | 81.80 | 21.872 | 2.3003 | 0.60 |
| 6 | 0 | 3 | 131.67 | 37.528 | 2.7550 | 0.13 |
| 6 | 1 | 4 | 115.25 | 9.179 | 3.3887 | 0.12 |
| 8 | 0 | 14 | 209.21 | 50.977 | 3.9992 | 0.76 |
| Data from the mtcars data set | ||||||
Optional post-processing:
| Motor Trend Car Road Tests | ||||||
| A table created with tablespan | ||||||
| Horse Power | Weight | |||||
| Cylinder | Engine | N | Mean | SD | Mean | SD |
| 4 | 0 | 1 | 91.00 | 2.1400 | ||
| 4 | 1 | 10 | 81.80 | 21.872 | 2.3003 | 0.60 |
| 6 | 0 | 3 | 131.67 | 37.528 | 2.7550 | 0.13 |
| 6 | 1 | 4 | 115.25 | 9.179 | 3.3887 | 0.12 |
| 8 | 0 | 14 | 209.21 | 50.977 | 3.9992 | 0.76 |
| Data from the mtcars data set | ||||||