Learn R Programming

⚠️There's a newer version (1.0.0) of this package.Take me there.

gt

With the gt package, anyone can make wonderful-looking tables using the R programming language. The gt philosophy: we can construct a wide variety of useful tables with a cohesive set of table parts. These include the table header, the stub, the column labels and spanner column labels, the table body, and the table footer.

It all begins with preprocessed table data (be it a tibble or a data frame). You then decide how to compose your gt table with the elements and formatting you need for the task at hand. Finally, the table is rendered by printing it at the console, including it in an R Markdown document, or exporting to a file using gtsave(). Currently, gt supports HTML output, with LaTeX and RTF planned for the future.

The gt package is designed to be both straightforward yet powerful. The emphasis is on simple functions for the everyday display table needs. Here is a brief example of how to use gt to create a table from the included sp500 dataset:

library(gt)
library(tidyverse)
library(glue)

# Define the start and end dates for the data range
start_date <- "2010-06-07"
end_date <- "2010-06-14"

# Create a gt table based on preprocessed
# `sp500` table data
sp500 %>%
  dplyr::filter(date >= start_date & date <= end_date) %>%
  dplyr::select(-adj_close) %>%
  gt() %>%
  tab_header(
    title = "S&P 500",
    subtitle = glue::glue("{start_date} to {end_date}")
  ) %>%
  fmt_date(
    columns = vars(date),
    date_style = 3
  ) %>%
  fmt_currency(
    columns = vars(open, high, low, close),
    currency = "USD"
  ) %>%
  fmt_number(
    columns = vars(volume),
    suffixing = TRUE
  )

There are six datasets included in gt: countrypops, sza, gtcars, sp500, pizzaplace, and exibble. All of them are useful for experimenting with gt’s functions.

Beyond this simple example, there are many functions available in gt for creating super-customized tables.

Want to try this out? Then, take the gt Test Drive on RStudio Cloud. It’s full of ready-to-run examples.

The gt package can be installed from CRAN with:

install.packages("gt")

You can also choose to install the development version of gt from GitHub:

devtools::install_github("rstudio/gt")

If you encounter a bug, have usage questions, or want to share ideas to make this package better, please feel free to file an issue.


How gt fits in with Other Packages that Generate Display Tables

The gt package joins a burgeoning collection of packages for display table generation. Why another? We feel that there is enough room in this space to innovate further. Here are some of the ways that gt contributes to this ecosystem:

  • the interface is high-level and declarative (general instructions versus very specific)
  • the formatting options are ‘batteries included’ (scientific notation, uncertainty, ranges, percentages, suffixes, localized currency, dates/times + much more)
  • there is excellent, pain-free support for footnotes
  • the output is ‘camera-ready’
  • it will eventually support multiple output formats (including LaTeX) with the same declarative interface
  • the API closely follows tidyverse conventions by adhering to the tidyverse style guide
  • a focus on making the package documentation and examples the best they can be
  • rigorous QA/QC measures: high test coverage for automated tests, and thorough manual testing by QA engineers (with every proposed code change)

While gt is trying to do something different with its own interface, it may not suit your specific needs. Here is a listing of leading table-making R packages, with links to their respective project pages:

knitr (GITHUBWEBSITE) — kableExtra (GITHUBWEBSITE) — formattable (GITHUBWEBSITE) — DT (GITHUBWEBSITE) — pander (GITHUBWEBSITE) — huxtable (GITHUBWEBSITE) — reactable (GITHUBWEBSITE) — flextable (GITHUBWEBSITE) — pixiedust (GITHUB) — tangram (GITHUB) — ztable (GITHUB) — condformat (GITHUB) — stargazer (CRAN) — xtable (CRAN)

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct.By participating in this project you agree to abide by its terms.

License

MIT © RStudio, PBC

Copy Link

Version

Install

install.packages('gt')

Monthly Downloads

252,322

Version

0.2.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Richard Iannone

Last Published

May 23rd, 2020

Functions in gt (0.2.1)

as.data.frame.gt_tbl

Transform a gt object to a data frame
cells_row_groups

Location helper for targeting row groups
cells_group

Location helper for targeting row groups (deprecated)
cells_grand_summary

Location helper for targeting cells in a grand summary
cells_column_spanners

Location helper for targeting the column spanners
cols_align

Set the alignment of columns
cells_title

Location helper for targeting the table title and subtitle
cells_stubhead

Location helper for targeting the table stubhead cell
cells_summary

Location helper for targeting group summary cells
cells_data

Location helper for targeting data cells in the table body (deprecated)
cols_merge

Merge data from two or more columns to a single column
cols_merge_range

Merge two columns to a value range column
cells_stub

Location helper for targeting cells in the table stub
cols_hide

Hide one or more columns
cols_move_to_end

Move one or more columns to the end
cols_move

Move one or more columns
cols_merge_uncert

Merge two columns to a value & uncertainty column
cols_width

Set the widths of columns
cols_label

Relabel one or more columns
countrypops

Yearly populations of countries from 1960 to 2017
cols_move_to_start

Move one or more columns to the start
fmt

Set a column format with a formatter function
escape_latex

Perform LaTeX escaping
fmt_currency

Format values as currencies
currency

Supply a custom currency symbol to fmt_currency()
extract_summary

Extract a summary list from a gt object
exibble

A toy example tibble for testing with gt: exibble
fmt_date

Format values as dates
fmt_markdown

Format Markdown text
fmt_datetime

Format values as date-times
data_color

Set data cell colors using a palette or a color function
gt-options

gt package options
fmt_passthrough

Format by simply passing data through
gt-package

gt: Easily Create Presentation-Ready Display Tables
fmt_percent

Format values as a percentage
fmt_time

Format values as times
fmt_scientific

Format values to scientific notation
grand_summary_rows

Add grand summary rows using aggregation functions
gt_output

Create a gt display table output element for Shiny
fmt_missing

Format missing values
ggplot_image

Helper function for adding a ggplot
fmt_number

Format numeric values
html

Interpret input text as HTML-formatted text
gtcars

Deluxe automobiles from the 2014-2017 period
info_paletteer

View a table with info on color palettes
info_currencies

View a table with info on supported currencies
info_time_style

View a table with info on time styles
info_date_style

View a table with info on date styles
gtsave

Save a gt table as a file
px

Helper for providing a numeric value as pixels value
gt

Create a gt table object
random_id

Helper for creating a random id for a gt table
row_group_order

Modify the ordering of any row groups
info_locales

View a table with info on supported locales
render_gt

A gt display table render function for use in Shiny
local_image

Helper function for adding a local image
md

Interpret input text as Markdown-formatted text
reexports

Objects exported from other packages
gt_preview

Preview a gt table object
opt_align_table_header

Option to align the table header
opt_row_striping

Option to add or remove row striping
tab_row_group

Add a row group to a gt table
tab_options

Modify the table output options
opt_footnote_marks

Option to modify the set of footnote marks
tab_footnote

Add a table footnote
gt_latex_dependencies

Get the LaTeX dependencies required for a gt table
sp500

Daily S&P 500 Index data from 1950 to 2015
tab_spanner

Add a spanner column label
tab_header

Add a table header
web_image

Helper function for adding an image from the web
text_transform

Perform targeted text transformation with a function
tab_source_note

Add a source note citation
pct

Helper for providing a numeric value as percentage
opt_all_caps

Option to use all caps in select table locations
%>%

Pipe operator
opt_table_lines

Option to set table lines to different extents
pizzaplace

A year of pizza sales from a pizza place
opt_table_outline

Option to wrap an outline around the entire table
sza

Twice hourly solar zenith angles by month & latitude
test_image

Generate a path to a test image
tab_style

Add custom styles to one or more cells
summary_rows

Add groupwise summary rows using aggregation functions
tab_stubhead

Add label text to the stubhead
tab_spanner_delim

Create column labels and spanners via delimited names
print.gt_tbl

Print the table
as_raw_html

Get the HTML content of a gt table
cells_column_labels

Location helper for targeting the column labels
cells_body

Location helper for targeting data cells in the table body
cell_fill

Helper for defining custom fills for table cells
as_latex

Output a gt object as LaTeX
adjust_luminance

Adjust the luminance for a palette of colors
as_rtf

Output a gt object as RTF
cell_text

Helper for defining custom text styles for table cells
cell_borders

Helper for defining custom borders for table cells