# gt — Publication-Quality Tables from R > gt is an R package for building display tables from data frames. It provides a structured grammar for adding headers, footers, row groups, spanners, formatting, and styling to produce tables suitable for reports, papers, and dashboards. ## Install Save in your project root: # gt — Publication-Quality Tables from R ## Quick Use ```r install.packages("gt") library(gt) mtcars[1:5, 1:4] |> gt() |> tab_header(title = "Motor Trend Cars", subtitle = "First 5 rows") |> fmt_number(columns = everything(), decimals = 1) ``` ## 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")` or `gtsave(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()`, and `tab_options()` ## Key Features - Composable API: build tables incrementally with `tab_*()`, `fmt_*()`, `cols_*()`, and `opt_*()` 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()` and `tab_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. ## Sources - https://github.com/rstudio/gt - https://gt.rstudio.com/ --- Source: https://tokrepo.com/en/workflows/asset-c98607eb Author: AI Open Source