Skip to main content

gt Integration

Gridwell integrates with gt to render gt table objects to all 11 output formats from R.

Quick Start

library(gt)
library(gridwell)

tbl <- gt(exibble, rowname_col = "row", groupname_col = "group") |>
  tab_header(title = "Example", subtitle = "From exibble")

html <- gw_render_html(tbl)

Every gridwell function takes a gt table and converts it on the way in. To convert once, use as_gw_table(), which gives a gw_table:

gw <- as_gw_table(tbl)
gw                    # <gw_table: 8 columns, 8 rows in 2 groups, titled "Example">
                      # and the table drawn as text

In R Markdown and Quarto, a gw_table a chunk returns is shown as the document’s format: HTML (with styles scoped to that table, and marked so Quarto doesn’t add Bootstrap’s table styling), LaTeX for PDF (asking for the LaTeX packages it uses), or Typst. Word and PowerPoint documents get the table drawn as text; to put a real table in one, save it with gw_save(tbl, "table.docx").

Functions

as_gw_table(x)

A gw_table from a gt table, IR as a JSON string, or a gw_table (returned as is).

gt_to_ir(gt_tbl, emitter = "auto")

Converts a gt_tbl object to gridwell IR JSON string.

ir_json <- gt_to_ir(tbl)

gt_to_ir_list(gt_tbl, emitter = "auto")

Converts a gt_tbl object to an R list (IR structure).

ir_list <- gt_to_ir_list(tbl)

Which converter

The converter is moving into gt, as gt::as_gridwell(), since it reads gt’s internals and gt can keep it in step with them. Until every gt has it, gridwell keeps its own copy. emitter picks one:

  • "auto" (the default): gt’s when the installed gt has as_gridwell(), and gridwell’s otherwise. If gt’s fails, or gives an IR major version this gridwell can’t read, gridwell’s is used with a warning saying why.
  • "upstream": gt’s, with an error if there is none or it can’t be used.
  • "gridwell": gridwell’s.

No released gt has as_gridwell() yet, so today "auto" always uses gridwell’s.

What Gets Converted

gt Feature IR Mapping
Column labels (_boxhead) thead rows
Row groups (_row_groups) tbody array with group labels
Stub column (rowname_col) Cells with is_stub: true, always aligned left
A stub of several columns (rowname_col = c("region", "city")) One stub column each, outermost first; consecutive rows of a group with the same outer value (and the same values to its left) share one cell spanning them, merged in the order the rows are shown
Row group column (row_group_as_column = TRUE) The first stub column: each group’s label is a stub cell spanning its rows, and the group has no label row. A summary row’s label spans the other stub columns, beside an empty cell under the group’s label (gt’s HTML has the group’s cell span its summary rows instead)
Stubhead label (tab_stubhead()) One head cell spanning the stub columns; a vector of labels gives each row stub column its own, with a blank one above a row group column
Title/subtitle (_heading) header block
Summary rows (summary_rows(), grand_summary_rows()) A group’s summary_rows, and a last group without a label for the grand summary. A group whose summary rows are all on its top side (side = "top") has them at the top (summary_placement: "top"); one with rows on both sides has them all at the bottom. A grand summary on the top side is a first group without a label, with its rows at the top
Table id (gt(id =)) config.id, which the HTML writer uses as its table_id when that option isn’t set; an id that isn’t an HTML id (empty, or with whitespace) is left out
Border options (tab_options(table.border.top.color = …) and the like) The table’s rules, where changed from gt’s defaults, as the Great Tables guide describes for the same options; styles the IR has no rule for (groove and the like) are left out
Source notes (_source_notes) footer.source_notes
Column alignment column_spec[].align
Column widths column_spec[].width
Data values Cell content and typed_value
Typed values The number displayed, read as the Python package reads it (in any locale’s marks; without its currency or unit; 0.451 for 45.1%; scaled for scientific and compact notation; none for a display that isn’t one number, such as <0.001). POSIXct date-times are typed as their wall-clock time in their own time zone, and hms times of day as times

Full Pipeline Example

library(gt)
library(gridwell)

tbl <- gt(exibble, rowname_col = "row", groupname_col = "group") |>
  tab_header(title = "Example", subtitle = "Demo") |>
  tab_source_note(source_note = "Source: gt package")

gw <- as_gw_table(tbl)

# Validate
errors <- gw_validate(gw)
stopifnot(length(errors) == 0)

# Text formats
for (fmt in c("html", "latex", "typst", "rtf", "svg", "ansi", "pandoc", "quarto")) {
  result <- gw_render(gw, fmt)
  cat(sprintf("  %s: %d chars\n", fmt, nchar(result)))
}

# Binary formats
for (fmt in c("docx", "xlsx", "pptx")) {
  data <- gw_render_binary(gw, fmt)
  cat(sprintf("  %s: %d bytes\n", fmt, length(data)))
}