Introduction
gt (short for Grammar of Tables) is an R package by Rich Iannone from Posit that provides a structured approach to building display tables. While data frames hold data, gt transforms them into presentation-ready tables with titles, footnotes, column spanners, row grouping, conditional formatting, and rich styling. It fills the gap between raw data output and the polished tables found in publications and reports.
What gt Does
- Converts data frames and tibbles into styled HTML, LaTeX, RTF, and Word tables
- Adds table headers, subtitles, source notes, and footnotes with dedicated functions
- Groups rows by category and adds summary rows with aggregation functions
- Formats numbers, currencies, percentages, dates, and scientific notation with
fmt_*()functions - Applies conditional cell styling (color, bold, background) based on data values
Architecture Overview
gt models a table as a layered object with distinct regions: title/subtitle, column labels, column spanners, the body (stub + cells), row group labels, summary rows, and source notes. Each region is configured independently through chainable function calls. Internally, gt maintains a list-based table specification that is rendered to the target format (HTML, LaTeX, RTF, DOCX) at print time. The rendering pipeline generates CSS-styled HTML by default or LaTeX tabular environments for PDF output.
Self-Hosting & Configuration
- Install from CRAN:
install.packages("gt") - Pipe data frames into
gt()and chain formatting and styling functions - Save tables to files with
gtsave(table, "output.html")orgtsave(table, "output.png") - Use in R Markdown / Quarto documents by printing gt objects in code chunks
- Customize themes globally with
opt_table_font(),opt_row_striping(), andtab_options()
Key Features
- Composable API: build tables incrementally with
tab_*(),fmt_*(),cols_*(), andopt_*()functions - Column spanners group related columns under shared headers for complex table layouts
- Summary rows compute group-level and grand-total aggregations displayed inline
- Conditional formatting with
data_color()andtab_style()highlights values based on rules - Multi-format output: the same gt object renders to HTML, LaTeX, RTF, Word, and PNG
Comparison with Similar Tools
- kableExtra (R) — extends kable with styling; gt offers a richer grammar and more output formats
- flextable (R) — targets Word and PowerPoint; gt is stronger in HTML and integrates better with the tidyverse
- reactable (R) — interactive sortable/filterable HTML tables; gt focuses on static publication-quality output
- DT (R) — wraps DataTables.js for interactive tables; gt is for polished static presentation rather than exploration
- pandas Styler (Python) — styles DataFrames for display; gt provides a more structured grammar with footers, spanners, and grouping
FAQ
Q: Can gt produce PDF tables?
A: Yes. gt renders to LaTeX which compiles to PDF. Use as_latex() or render within an R Markdown PDF document.
Q: Does gt work in Shiny applications?
A: Yes. Use render_gt() and gt_output() in Shiny to display gt tables reactively.
Q: How do I add color scales to a column?
A: Use data_color(columns = value_col, palette = "viridis") to apply a continuous color scale based on cell values.
Q: Can I export gt tables as images?
A: Yes. gtsave(table, "table.png") renders the table as a PNG image using the webshot2 package.