Creating beautiful tables in R with {gt}

Author

Albert Rapp

Published

October 30, 2024

Introduction

Tables don’t have to be dull lists of numbers. If done correctly, they can, in fact, be beautiful & engaging. And in this book, we’re going to learn how to create such tables with R. More precisely, we will learn how to use the {gt} package.

1  Getting started is the starting point of our {gt} journey. It will cover useful guidelines for any table and how to implement them with {gt}. You can think of this chapter as a quickstart guide.

Afterwards, we’re learning how to add eye-catching elements like plots, icons and images to our table in 2  Fancy stuff / Eye catchers. Many of these elements are powered through the amazing {gtExtras} package. But you will also learn how to add any plot to your table (regardless of whether it is implemented in {gtExtras} or not.)

Next, we’ll talk about formatting the entries of your table in 3  Formatting. {gt} has two powerful sets of functions to get this job done, namely fmt_*() and sub_*(). These functions all work the same and are pretty easy to pick up. Hence, it’s easy to format the data in your table the way you want or need.

In 4  Styling, we’re learning how to change the theme of our table. This will be a long chapter. After all, we can basically change anything about our table’s appearance.

All of the knowledge that we’ve acquired in the previous chapters culminate in 5  Case studies. That’s where we will build elaborate tables with {gt}.

Finally, 6  Quarto and {gt} is a special chapter dedicated to the interactions between Quarto and {gt}. As of right now (Quarto v1.2.267 & {gt} v0.8.0), Quarto’s default theme can interact with your {gt} table and change its theme. But there are ways to isolate Quarto from {gt}. This is what you’ll learn in this chapter.

Prerequisites

This book is meant for people with an intermediate knowledge of R, specifically the Tidyverse. I do not expect people to know HTML/CSS for the most parts.

Throughout most of the book, we’ll only to do small changes with HTML/CSS and these bits will be explained. But there will be parts in 4  Styling and the subsequent chapters that will talk about changing CSS styles. Feel free to skip these parts if you’re uncomfortable with CSS. But I’ll do my best to help you with CSS.

About this book

This book is completely free and the code is open-source. If you find the things you learn here useful, I’d appreciate it if you sponsor my work via Buy Me Coffee.

Also, if you’re interested in working with me, I’m open to freelance work. You can book an appointment with me on Calendly.

Session info

Just so you’re sure what versions I’m using. Here’s my session info and my Quarto version.

sessioninfo::session_info("installed")
## ─ Session info ───────────────────────────────────────────────────────────────
##  setting  value
##  version  R version 4.4.1 (2024-06-14)
##  os       Ubuntu 22.04.4 LTS
##  system   x86_64, linux-gnu
##  ui       X11
##  language (EN)
##  collate  en_US.UTF-8
##  ctype    en_US.UTF-8
##  tz       Europe/Berlin
##  date     2024-10-30
##  pandoc   3.2 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/x86_64/ (via rmarkdown)
## 
## ─ Packages ───────────────────────────────────────────────────────────────────
##  ! package        * version    date (UTC) lib source
##    AsioHeaders      1.22.1-1   2022-06-14 [1] CRAN (R 4.4.1)
##    askpass          1.1        2019-01-13 [1] CRAN (R 4.4.0)
##    assertthat       0.2.1      2019-03-21 [1] CRAN (R 4.4.0)
##    backports        1.4.1      2021-12-13 [1] CRAN (R 4.4.0)
##    base64enc        0.1-3      2015-07-28 [1] CRAN (R 4.4.0)
##    bigD             0.2.0      2022-09-05 [1] CRAN (R 4.4.0)
##    bit              4.0.4      2020-08-04 [1] CRAN (R 4.4.0)
##    bit64            4.0.5      2020-08-30 [1] CRAN (R 4.4.0)
##    bitops           1.0-7      2021-04-24 [1] CRAN (R 4.4.0)
##    blob             1.2.3      2022-04-10 [1] RSPM (R 4.4.0)
##    boot             1.3-30     2024-02-26 [2] CRAN (R 4.3.3)
##    broom            1.0.1      2022-08-29 [1] RSPM (R 4.4.1)
##    bslib            0.4.1      2022-11-02 [1] RSPM (R 4.4.1)
##    cachem           1.0.6      2021-08-19 [1] RSPM (R 4.4.0)
##    callr            3.7.3      2022-11-02 [1] CRAN (R 4.4.0)
##    cellranger       1.1.0      2016-07-27 [1] CRAN (R 4.4.0)
##    chromote         0.1.1      2022-09-07 [1] RSPM (R 4.4.0)
##    class            7.3-22     2023-05-03 [2] CRAN (R 4.3.1)
##  P cli              3.6.3      2024-06-21 [?] RSPM (R 4.4.0)
##    clipr            0.8.0      2022-02-22 [1] CRAN (R 4.4.0)
##    cluster          2.1.6      2023-12-01 [2] CRAN (R 4.3.2)
##    codetools        0.2-19     2023-02-01 [2] CRAN (R 4.2.2)
##    colorspace       2.0-3      2022-02-21 [1] RSPM (R 4.4.0)
##    commonmark       1.9.2      2024-10-04 [1] CRAN (R 4.4.1)
##    cpp11            0.4.3      2022-10-12 [1] RSPM (R 4.4.0)
##    crayon           1.5.2      2022-09-29 [1] CRAN (R 4.4.0)
##    curl             4.3.3      2022-10-06 [1] RSPM (R 4.4.0)
##    data.table       1.14.4     2022-10-17 [1] RSPM (R 4.4.0)
##    DBI              1.1.3      2022-06-18 [1] CRAN (R 4.4.1)
##    dbplyr           2.2.1      2022-06-27 [1] RSPM (R 4.4.0)
##  P digest           0.6.30     2022-10-18 [?] RSPM (R 4.4.0)
##    dplyr            1.1.4      2023-11-17 [1] CRAN (R 4.4.0)
##    dtplyr           1.2.2      2022-08-20 [1] RSPM (R 4.4.0)
##    ellipsis         0.3.2      2021-04-29 [1] CRAN (R 4.4.0)
##    emoji            15.0       2022-11-03 [1] RSPM (R 4.4.0)
##  P evaluate         0.18       2022-11-07 [?] RSPM (R 4.4.0)
##    exams            2.4-0      2022-10-17 [1] RSPM (R 4.4.0)
##    fansi            1.0.3      2022-03-24 [1] RSPM (R 4.4.0)
##    farver           2.1.1      2022-07-06 [1] CRAN (R 4.4.0)
##  P fastmap          1.1.0      2021-01-25 [?] RSPM (R 4.4.0)
##    fontawesome      0.5.2      2023-08-19 [1] CRAN (R 4.4.0)
##    forcats          0.5.2      2022-08-19 [1] RSPM (R 4.4.0)
##    foreign          0.8-86     2023-11-28 [2] CRAN (R 4.3.2)
##    fs               1.6.4      2024-04-25 [1] CRAN (R 4.4.0)
##    gapminder        0.3.0      2017-10-31 [1] RSPM (R 4.4.0)
##    gargle           1.2.1      2022-09-08 [1] RSPM (R 4.4.1)
##    generics         0.1.3      2022-07-05 [1] CRAN (R 4.4.0)
##    ggplot2          3.5.1      2024-04-23 [1] CRAN (R 4.4.0)
##    glue             1.8.0      2024-09-30 [1] RSPM (R 4.4.0)
##    googledrive      2.0.0      2021-07-08 [1] RSPM (R 4.4.0)
##    googlesheets4    1.0.1      2022-08-13 [1] RSPM (R 4.4.1)
##    gt               0.11.1     2024-10-04 [1] CRAN (R 4.4.1)
##    gtable           0.3.1      2022-09-01 [1] RSPM (R 4.4.0)
##    gtExtras         0.4.3      2022-11-05 [1] RSPM (R 4.4.1)
##    haven            2.5.4      2023-11-30 [1] CRAN (R 4.4.0)
##    here             1.0.1      2020-12-13 [1] CRAN (R 4.4.0)
##    highr            0.11       2024-05-26 [1] RSPM (R 4.4.0)
##    hms              1.1.2      2022-08-19 [1] RSPM (R 4.4.0)
##  P htmltools        0.5.8.1    2024-04-04 [?] CRAN (R 4.4.0)
##  P htmlwidgets      1.6.4      2023-12-06 [?] CRAN (R 4.4.0)
##    httr             1.4.4      2022-08-17 [1] RSPM (R 4.4.1)
##    ids              1.0.1      2017-05-31 [1] CRAN (R 4.4.0)
##    isoband          0.2.6      2022-10-06 [1] RSPM (R 4.4.0)
##    janitor          2.1.0      2021-01-05 [1] RSPM (R 4.4.0)
##    jquerylib        0.1.4      2021-04-26 [1] CRAN (R 4.4.0)
##  P jsonlite         1.8.9      2024-09-20 [?] RSPM (R 4.4.0)
##    juicyjuice       0.1.0      2022-11-10 [1] CRAN (R 4.4.0)
##    KernSmooth       2.23-24    2024-05-17 [2] CRAN (R 4.4.0)
##  P knitr            1.48       2024-07-07 [?] RSPM (R 4.4.0)
##    labeling         0.4.2      2020-10-20 [1] RSPM (R 4.4.0)
##    later            1.3.0      2021-08-18 [1] RSPM (R 4.4.0)
##    lattice          0.20-45    2021-09-22 [1] CRAN (R 4.4.1)
##    lifecycle        1.0.3      2022-10-07 [1] RSPM (R 4.4.0)
##    lubridate        1.9.0      2022-11-06 [1] RSPM (R 4.4.0)
##    magrittr         2.0.3      2022-03-30 [1] CRAN (R 4.4.0)
##    markdown         1.13       2024-06-04 [1] RSPM (R 4.4.0)
##    MASS             7.3-58.1   2022-08-03 [1] CRAN (R 4.4.1)
##    Matrix           1.5-3      2022-11-11 [1] CRAN (R 4.4.1)
##    memoise          2.0.1      2021-11-26 [1] CRAN (R 4.4.0)
##    mgcv             1.8-41     2022-10-21 [1] CRAN (R 4.4.1)
##    mime             0.12       2021-09-28 [1] CRAN (R 4.4.0)
##    modelr           0.1.9      2022-08-19 [1] RSPM (R 4.4.1)
##    munsell          0.5.0      2018-06-12 [1] RSPM (R 4.4.0)
##    nlme             3.1-160    2022-10-10 [1] CRAN (R 4.4.1)
##    nnet             7.3-19     2023-05-03 [2] CRAN (R 4.3.1)
##    openssl          2.0.4      2022-10-17 [1] RSPM (R 4.4.0)
##    paletteer        1.5.0      2022-10-19 [1] RSPM (R 4.4.0)
##    palmerpenguins   0.1.1      2022-08-15 [1] RSPM (R 4.4.0)
##    pillar           1.9.0      2023-03-22 [1] CRAN (R 4.4.0)
##    pkgconfig        2.0.3      2019-09-22 [1] CRAN (R 4.4.0)
##    prettyunits      1.1.1      2020-01-24 [1] RSPM (R 4.4.0)
##    prismatic        1.1.1      2022-08-15 [1] RSPM (R 4.4.0)
##    processx         3.8.0      2022-10-26 [1] RSPM (R 4.4.0)
##    progress         1.2.2      2019-05-16 [1] RSPM (R 4.4.0)
##    promises         1.2.0.1    2021-02-11 [1] RSPM (R 4.4.0)
##    ps               1.7.2      2022-10-26 [1] RSPM (R 4.4.0)
##    purrr            0.3.5      2022-10-06 [1] RSPM (R 4.4.0)
##    R6               2.5.1      2021-08-19 [1] CRAN (R 4.4.0)
##    rappdirs         0.3.3      2021-01-31 [1] CRAN (R 4.4.0)
##    RColorBrewer     1.1-3      2022-04-03 [1] CRAN (R 4.4.0)
##    Rcpp             1.0.9      2022-07-08 [1] RSPM (R 4.4.0)
##    reactable        0.4.4      2023-03-12 [1] CRAN (R 4.4.0)
##    reactR           0.6.1      2024-09-14 [1] CRAN (R 4.4.1)
##    readr            2.1.3      2022-10-01 [1] RSPM (R 4.4.0)
##    readxl           1.4.1      2022-08-17 [1] RSPM (R 4.4.0)
##    rematch          1.0.1      2016-04-21 [1] CRAN (R 4.4.0)
##    rematch2         2.1.2      2020-05-01 [1] CRAN (R 4.4.0)
##    renv             0.15.5     2022-05-26 [1] CRAN (R 4.4.1)
##    reprex           2.0.2      2022-08-17 [1] RSPM (R 4.4.0)
##  P rlang            1.1.4      2024-06-04 [?] RSPM (R 4.4.0)
##  P rmarkdown        2.28       2024-08-17 [?] RSPM (R 4.4.0)
##    rpart            4.1.23     2023-12-05 [2] CRAN (R 4.3.2)
##    rprojroot        2.0.3      2022-04-02 [1] RSPM (R 4.4.0)
##  P rstudioapi       0.14       2022-08-22 [?] RSPM (R 4.4.0)
##    rvest            1.0.3      2022-08-19 [1] RSPM (R 4.4.0)
##    sass             0.4.9      2024-03-15 [1] CRAN (R 4.4.0)
##    scales           1.3.0      2023-11-28 [1] CRAN (R 4.4.0)
##    selectr          0.4-2      2019-11-20 [1] CRAN (R 4.4.0)
##  P sessioninfo      1.2.2      2021-12-06 [?] RSPM (R 4.4.0)
##    snakecase        0.11.0     2019-05-25 [1] RSPM (R 4.4.0)
##    spatial          7.3-15     2022-01-13 [2] CRAN (R 4.2.0)
##    stringi          1.7.8      2022-07-11 [1] RSPM (R 4.4.0)
##    stringr          1.4.1.9000 2024-10-26 [1] Github (tidyverse/stringr@f482fb0)
##    survival         3.7-0      2024-06-05 [2] CRAN (R 4.4.0)
##    svglite          2.1.0      2022-02-03 [1] RSPM (R 4.4.0)
##    sys              3.4.1      2022-10-18 [1] RSPM (R 4.4.0)
##    systemfonts      1.0.4      2022-02-11 [1] RSPM (R 4.4.0)
##    thematic         0.1.2.1    2021-06-09 [1] RSPM (R 4.4.1)
##    tibble           3.2.1      2023-03-20 [1] CRAN (R 4.4.0)
##    tidyr            1.2.1      2022-09-08 [1] RSPM (R 4.4.0)
##    tidyselect       1.2.1      2024-03-11 [1] CRAN (R 4.4.0)
##    tidyverse        1.3.2      2022-07-18 [1] RSPM (R 4.4.1)
##    timechange       0.1.1      2022-11-04 [1] RSPM (R 4.4.0)
##    tinytex          0.53       2024-09-15 [1] RSPM (R 4.4.0)
##    tzdb             0.3.0      2022-03-28 [1] RSPM (R 4.4.0)
##    utf8             1.2.2      2021-07-24 [1] RSPM (R 4.4.0)
##    uuid             1.1-0      2022-04-19 [1] RSPM (R 4.4.0)
##    V8               4.4.0      2023-10-09 [1] RSPM (R 4.4.0)
##    vctrs            0.6.5      2023-12-01 [1] CRAN (R 4.4.0)
##    viridisLite      0.4.1      2022-08-22 [1] RSPM (R 4.4.0)
##    vroom            1.6.0      2022-09-30 [1] RSPM (R 4.4.0)
##    webshot2         0.1.0      2022-05-18 [1] RSPM (R 4.4.0)
##    websocket        1.4.1      2021-08-18 [1] RSPM (R 4.4.0)
##    withr            2.5.0      2022-03-03 [1] RSPM (R 4.4.0)
##  P xfun             0.48       2024-10-03 [?] RSPM (R 4.4.0)
##    xml2             1.3.6      2023-12-04 [1] CRAN (R 4.4.0)
##    yaml             2.3.6      2022-10-18 [1] RSPM (R 4.4.0)
## 
##  [1] /home/albert/Nextcloud/R Projects/gt_book/renv/library/R-4.4/x86_64-pc-linux-gnu
##  [2] /usr/lib/R/library
## 
##  P ── Loaded and on-disk path mismatch.
## 
## ──────────────────────────────────────────────────────────────────────────────
quarto check
## Quarto 1.6.4
## 
[✓] Checking versions of quarto binary dependencies...
##       Pandoc version 3.2.0: OK
##       Dart Sass version 1.70.0: OK
##       Deno version 1.41.0: OK
##       Typst version 0.11.0: OK
## 
[✓] Checking versions of quarto dependencies......OK
## 
[✓] Checking Quarto installation......OK
##       Version: 1.6.4
##       Path: /opt/quarto/bin
## 
## 
(|) Checking tools....................
[✓] Checking tools....................OK
##       TinyTeX: (not installed)
##       Chromium: (not installed)
## 
## 
(|) Checking LaTeX....................
(/) Checking LaTeX....................
(-) Checking LaTeX....................
(\) Checking LaTeX....................
[✓] Checking LaTeX....................OK
##       Using: Installation From Path
##       Path: /usr/bin
##       Version: 2021
## 
## 
(|) Checking basic markdown render....
(/) Checking basic markdown render....
(-) Checking basic markdown render....
(\) Checking basic markdown render....
[✓] Checking basic markdown render....OK
## 
## 
[✓] Checking Python 3 installation....OK
##       Version: 3.11.5 (Conda)
##       Path: /home/albert/anaconda3/bin/python
##       Jupyter: 5.3.0
##       Kernels: python3
## 
## 
(|) Checking Jupyter engine render....
(/) Checking Jupyter engine render....
(-) Checking Jupyter engine render....
(\) Checking Jupyter engine render....
(|) Checking Jupyter engine render....
(/) Checking Jupyter engine render....
(-) Checking Jupyter engine render....
(\) Checking Jupyter engine render....
(|) Checking Jupyter engine render....
(/) Checking Jupyter engine render....
(-) Checking Jupyter engine render....
(\) Checking Jupyter engine render....
(|) Checking Jupyter engine render....
(/) Checking Jupyter engine render....
(-) Checking Jupyter engine render....
(\) Checking Jupyter engine render....
[✓] Checking Jupyter engine render....OK
## 
## 
(|) Checking R installation...........
(/) Checking R installation...........
[✓] Checking R installation...........OK
##       Version: 4.4.1
##       Path: /usr/lib/R
##       LibPaths:
##         - /home/albert/Nextcloud/R Projects/gt_book/renv/library/R-4.4/x86_64-pc-linux-gnu
##         - /usr/lib/R/library
##       knitr: 1.48
##       rmarkdown: 2.28
## 
## 
(|) Checking Knitr engine render......
(/) Checking Knitr engine render......
(-) Checking Knitr engine render......
(\) Checking Knitr engine render......
(|) Checking Knitr engine render......
(/) Checking Knitr engine render......
[✓] Checking Knitr engine render......OK