Skip to contents

Add a caption to a gt table, which is handled specially for a table within an R Markdown, Quarto, or bookdown context. The addition of captions makes tables cross-referencing across the containing document. The caption location (i.e., top, bottom, margin) is handled at the document level in each of these system.

Usage

tab_caption(data, caption)

Arguments

data

The gt table data object

obj:<gt_tbl> // required

This is the gt table object that is commonly created through use of the gt() function.

caption

Table caption text

scalar<character> // required

The table caption to use for cross-referencing in R Markdown, Quarto, or bookdown.

Value

An object of class gt_tbl.

Examples

With three columns from the gtcars dataset, let's create a gt table. First, we'll add a header part with the tab_header() function. After that, a caption is added through use of tab_caption().

gtcars |>
  dplyr::select(mfr, model, msrp) |>
  dplyr::slice(1:5) |>
  gt() |>
  tab_header(
    title = md("Data listing from **gtcars**"),
    subtitle = md("`gtcars` is an R dataset")
  ) |>
  tab_caption(caption = md("**gt** table example."))

This image of a table was generated from the first code example in the `tab_caption()` help file.

Function ID

2-9

Function Introduced

v0.8.0 (November 16, 2022)

See also