gt Tables Exercises in R: 22 Real-World Practice Problems

Twenty-two practice problems for building publication-grade tables with the gt package in R. Each problem comes with a domain-flavoured task, an expected output, and a hidden solution you reveal only after you write your own. Topics: table construction, headers and spanners, number and date formatting, conditional styling, summary rows, row groups, and saving the final artifact.

RRun this once before any exercise
library(gt) library(dplyr) library(tibble) set.seed(42)

Section 1. Building basic gt tables (3 problems)

Exercise 1.1: Turn a tibble into a basic gt table

Task: A reporting analyst wants the simplest possible publication object for a small mtcars slice. Take the first 5 rows of mtcars, keep only mpg, cyl, and hp, move the rownames into a model column, and pipe the result into gt(). Save to ex_1_1.

Expected result:

<div id="qeayjdrzqo" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#qeayjdrzqo table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#qeayjdrzqo thead, #qeayjdrzqo tbody, #qeayjdrzqo tfoot, #qeayjdrzqo tr, #qeayjdrzqo td, #qeayjdrzqo th {
  border-style: none;
}

#qeayjdrzqo p {
  margin: 0;
  padding: 0;
}

#qeayjdrzqo .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#qeayjdrzqo .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#qeayjdrzqo .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#qeayjdrzqo .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#qeayjdrzqo .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#qeayjdrzqo .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qeayjdrzqo .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#qeayjdrzqo .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#qeayjdrzqo .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#qeayjdrzqo .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#qeayjdrzqo .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#qeayjdrzqo .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#qeayjdrzqo .gt_spanner_row {
  border-bottom-style: hidden;
}

#qeayjdrzqo .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#qeayjdrzqo .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#qeayjdrzqo .gt_from_md > :first-child {
  margin-top: 0;
}

#qeayjdrzqo .gt_from_md > :last-child {
  margin-bottom: 0;
}

#qeayjdrzqo .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#qeayjdrzqo .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#qeayjdrzqo .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#qeayjdrzqo .gt_row_group_first td {
  border-top-width: 2px;
}

#qeayjdrzqo .gt_row_group_first th {
  border-top-width: 2px;
}

#qeayjdrzqo .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#qeayjdrzqo .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#qeayjdrzqo .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#qeayjdrzqo .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qeayjdrzqo .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#qeayjdrzqo .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#qeayjdrzqo .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#qeayjdrzqo .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#qeayjdrzqo .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qeayjdrzqo .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#qeayjdrzqo .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#qeayjdrzqo .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#qeayjdrzqo .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#qeayjdrzqo .gt_left {
  text-align: left;
}

#qeayjdrzqo .gt_center {
  text-align: center;
}

#qeayjdrzqo .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#qeayjdrzqo .gt_font_normal {
  font-weight: normal;
}

#qeayjdrzqo .gt_font_bold {
  font-weight: bold;
}

#qeayjdrzqo .gt_font_italic {
  font-style: italic;
}

#qeayjdrzqo .gt_super {
  font-size: 65%;
}

#qeayjdrzqo .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#qeayjdrzqo .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#qeayjdrzqo .gt_indent_1 {
  text-indent: 5px;
}

#qeayjdrzqo .gt_indent_2 {
  text-indent: 10px;
}

#qeayjdrzqo .gt_indent_3 {
  text-indent: 15px;
}

#qeayjdrzqo .gt_indent_4 {
  text-indent: 20px;
}

#qeayjdrzqo .gt_indent_5 {
  text-indent: 25px;
}

#qeayjdrzqo .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#qeayjdrzqo div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="model">model</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="mpg">mpg</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="cyl">cyl</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="hp">hp</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><td headers="model" class="gt_row gt_left">Mazda RX4</td>
<td headers="mpg" class="gt_row gt_right">21.0</td>
<td headers="cyl" class="gt_row gt_right">6</td>
<td headers="hp" class="gt_row gt_right">110</td></tr>
    <tr><td headers="model" class="gt_row gt_left">Mazda RX4 Wag</td>
<td headers="mpg" class="gt_row gt_right">21.0</td>
<td headers="cyl" class="gt_row gt_right">6</td>
<td headers="hp" class="gt_row gt_right">110</td></tr>
    <tr><td headers="model" class="gt_row gt_left">Datsun 710</td>
<td headers="mpg" class="gt_row gt_right">22.8</td>
<td headers="cyl" class="gt_row gt_right">4</td>
<td headers="hp" class="gt_row gt_right">93</td></tr>
    <tr><td headers="model" class="gt_row gt_left">Hornet 4 Drive</td>
<td headers="mpg" class="gt_row gt_right">21.4</td>
<td headers="cyl" class="gt_row gt_right">6</td>
<td headers="hp" class="gt_row gt_right">110</td></tr>
    <tr><td headers="model" class="gt_row gt_left">Hornet Sportabout</td>
<td headers="mpg" class="gt_row gt_right">18.7</td>
<td headers="cyl" class="gt_row gt_right">8</td>
<td headers="hp" class="gt_row gt_right">175</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Beginner

RYour turn
ex_1_1 <- # your code here ex_1_1

Click to reveal solution
RSolution
ex_1_1 <- mtcars |> tibble::rownames_to_column("model") |> head(5) |> select(model, mpg, cyl, hp) |> gt() ex_1_1 #> An HTML table with 5 rows and columns: model, mpg, cyl, hp

Explanation: gt() accepts any data frame or tibble and returns a gt_tbl object that renders as HTML. Because mtcars stores model names in row names, rownames_to_column() lifts them into a real column before you call gt(), which is critical: gt() silently drops row names. Pipe order matters here, slice first or after the rename, but the rename must precede gt().

Exercise 1.2: Use a rowname column with gt for a clean stub

Task: The reporting analyst now wants the model name to appear as a row label (the "stub") rather than a regular column. Take the first 5 rows of mtcars, keep mpg and cyl, and pass rowname_col = "model" to gt() so the model becomes the stub. Save the table to ex_1_2.

Expected result:

<div id="xgdyentzro" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#xgdyentzro table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#xgdyentzro thead, #xgdyentzro tbody, #xgdyentzro tfoot, #xgdyentzro tr, #xgdyentzro td, #xgdyentzro th {
  border-style: none;
}

#xgdyentzro p {
  margin: 0;
  padding: 0;
}

#xgdyentzro .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#xgdyentzro .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#xgdyentzro .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#xgdyentzro .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#xgdyentzro .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#xgdyentzro .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#xgdyentzro .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#xgdyentzro .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#xgdyentzro .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#xgdyentzro .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#xgdyentzro .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#xgdyentzro .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#xgdyentzro .gt_spanner_row {
  border-bottom-style: hidden;
}

#xgdyentzro .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#xgdyentzro .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#xgdyentzro .gt_from_md > :first-child {
  margin-top: 0;
}

#xgdyentzro .gt_from_md > :last-child {
  margin-bottom: 0;
}

#xgdyentzro .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#xgdyentzro .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#xgdyentzro .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#xgdyentzro .gt_row_group_first td {
  border-top-width: 2px;
}

#xgdyentzro .gt_row_group_first th {
  border-top-width: 2px;
}

#xgdyentzro .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#xgdyentzro .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#xgdyentzro .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#xgdyentzro .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#xgdyentzro .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#xgdyentzro .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#xgdyentzro .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#xgdyentzro .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#xgdyentzro .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#xgdyentzro .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#xgdyentzro .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#xgdyentzro .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#xgdyentzro .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#xgdyentzro .gt_left {
  text-align: left;
}

#xgdyentzro .gt_center {
  text-align: center;
}

#xgdyentzro .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#xgdyentzro .gt_font_normal {
  font-weight: normal;
}

#xgdyentzro .gt_font_bold {
  font-weight: bold;
}

#xgdyentzro .gt_font_italic {
  font-style: italic;
}

#xgdyentzro .gt_super {
  font-size: 65%;
}

#xgdyentzro .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#xgdyentzro .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#xgdyentzro .gt_indent_1 {
  text-indent: 5px;
}

#xgdyentzro .gt_indent_2 {
  text-indent: 10px;
}

#xgdyentzro .gt_indent_3 {
  text-indent: 15px;
}

#xgdyentzro .gt_indent_4 {
  text-indent: 20px;
}

#xgdyentzro .gt_indent_5 {
  text-indent: 25px;
}

#xgdyentzro .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#xgdyentzro div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="mpg">mpg</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="cyl">cyl</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">Mazda RX4</th>
<td headers="stub_1_1 mpg" class="gt_row gt_right">21.0</td>
<td headers="stub_1_1 cyl" class="gt_row gt_right">6</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">Mazda RX4 Wag</th>
<td headers="stub_1_2 mpg" class="gt_row gt_right">21.0</td>
<td headers="stub_1_2 cyl" class="gt_row gt_right">6</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">Datsun 710</th>
<td headers="stub_1_3 mpg" class="gt_row gt_right">22.8</td>
<td headers="stub_1_3 cyl" class="gt_row gt_right">4</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_left gt_stub">Hornet 4 Drive</th>
<td headers="stub_1_4 mpg" class="gt_row gt_right">21.4</td>
<td headers="stub_1_4 cyl" class="gt_row gt_right">6</td></tr>
    <tr><th id="stub_1_5" scope="row" class="gt_row gt_left gt_stub">Hornet Sportabout</th>
<td headers="stub_1_5 mpg" class="gt_row gt_right">18.7</td>
<td headers="stub_1_5 cyl" class="gt_row gt_right">8</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Beginner

RYour turn
ex_1_2 <- # your code here ex_1_2

Click to reveal solution
RSolution
ex_1_2 <- mtcars |> tibble::rownames_to_column("model") |> head(5) |> select(model, mpg, cyl) |> gt(rowname_col = "model") ex_1_2 #> HTML table with stub = model, 2 data columns

Explanation: The stub is gt's special leftmost label column. Passing rowname_col at construction time turns that column into the stub, which unlocks features like summary rows, row group labels, and the cells_stub() location helper for tab_style(). A regular column has none of those affordances, so the choice between "data column" and "stub" sets the entire downstream styling vocabulary.

Exercise 1.3: Construct a gt table from an inline tibble of weekly KPIs

Task: The growth team needs a quick weekly snapshot for stand-up. Construct an inline tibble with columns week (W1 to W4), signups (320, 410, 388, 502), and revenue_usd (4820, 6105, 5740, 7888), then turn it into a basic gt table with week as the stub. Save to ex_1_3.

Expected result:

<div id="ciydemetbh" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#ciydemetbh table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#ciydemetbh thead, #ciydemetbh tbody, #ciydemetbh tfoot, #ciydemetbh tr, #ciydemetbh td, #ciydemetbh th {
  border-style: none;
}

#ciydemetbh p {
  margin: 0;
  padding: 0;
}

#ciydemetbh .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#ciydemetbh .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#ciydemetbh .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#ciydemetbh .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#ciydemetbh .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#ciydemetbh .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ciydemetbh .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#ciydemetbh .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#ciydemetbh .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#ciydemetbh .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#ciydemetbh .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#ciydemetbh .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#ciydemetbh .gt_spanner_row {
  border-bottom-style: hidden;
}

#ciydemetbh .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#ciydemetbh .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#ciydemetbh .gt_from_md > :first-child {
  margin-top: 0;
}

#ciydemetbh .gt_from_md > :last-child {
  margin-bottom: 0;
}

#ciydemetbh .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#ciydemetbh .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#ciydemetbh .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#ciydemetbh .gt_row_group_first td {
  border-top-width: 2px;
}

#ciydemetbh .gt_row_group_first th {
  border-top-width: 2px;
}

#ciydemetbh .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#ciydemetbh .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#ciydemetbh .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#ciydemetbh .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ciydemetbh .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#ciydemetbh .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#ciydemetbh .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#ciydemetbh .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#ciydemetbh .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ciydemetbh .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#ciydemetbh .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#ciydemetbh .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#ciydemetbh .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#ciydemetbh .gt_left {
  text-align: left;
}

#ciydemetbh .gt_center {
  text-align: center;
}

#ciydemetbh .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#ciydemetbh .gt_font_normal {
  font-weight: normal;
}

#ciydemetbh .gt_font_bold {
  font-weight: bold;
}

#ciydemetbh .gt_font_italic {
  font-style: italic;
}

#ciydemetbh .gt_super {
  font-size: 65%;
}

#ciydemetbh .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#ciydemetbh .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#ciydemetbh .gt_indent_1 {
  text-indent: 5px;
}

#ciydemetbh .gt_indent_2 {
  text-indent: 10px;
}

#ciydemetbh .gt_indent_3 {
  text-indent: 15px;
}

#ciydemetbh .gt_indent_4 {
  text-indent: 20px;
}

#ciydemetbh .gt_indent_5 {
  text-indent: 25px;
}

#ciydemetbh .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#ciydemetbh div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="signups">signups</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="revenue_usd">revenue_usd</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">W1</th>
<td headers="stub_1_1 signups" class="gt_row gt_right">320</td>
<td headers="stub_1_1 revenue_usd" class="gt_row gt_right">4820</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">W2</th>
<td headers="stub_1_2 signups" class="gt_row gt_right">410</td>
<td headers="stub_1_2 revenue_usd" class="gt_row gt_right">6105</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">W3</th>
<td headers="stub_1_3 signups" class="gt_row gt_right">388</td>
<td headers="stub_1_3 revenue_usd" class="gt_row gt_right">5740</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_left gt_stub">W4</th>
<td headers="stub_1_4 signups" class="gt_row gt_right">502</td>
<td headers="stub_1_4 revenue_usd" class="gt_row gt_right">7888</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Beginner

RYour turn
ex_1_3 <- # your code here ex_1_3

Click to reveal solution
RSolution
kpi <- tibble( week = c("W1", "W2", "W3", "W4"), signups = c(320, 410, 388, 502), revenue_usd = c(4820, 6105, 5740, 7888) ) ex_1_3 <- gt(kpi, rowname_col = "week") ex_1_3 #> HTML table with stub = week and 2 data columns

Explanation: Inline tibbles are how analysts prototype gt outputs without dragging in a large dataset. Building the data with tibble() and piping directly into gt() is a fast iteration loop for stand-up reports. Once the styling is locked, you swap the inline tibble for the real query result with one line and the table format stays identical.

Section 2. Headers, spanners, and column labels (4 problems)

Exercise 2.1: Add a title and subtitle with tab_header

Task: The product manager asked for a labelled monthly report. Take ex_1_3 and add a title "Weekly Growth Snapshot" plus a subtitle "Signups and revenue, last 4 weeks" using tab_header(). Save the result to ex_2_1.

Expected result:

<div id="czajxkovzh" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#czajxkovzh table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#czajxkovzh thead, #czajxkovzh tbody, #czajxkovzh tfoot, #czajxkovzh tr, #czajxkovzh td, #czajxkovzh th {
  border-style: none;
}

#czajxkovzh p {
  margin: 0;
  padding: 0;
}

#czajxkovzh .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#czajxkovzh .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#czajxkovzh .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#czajxkovzh .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#czajxkovzh .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#czajxkovzh .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#czajxkovzh .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#czajxkovzh .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#czajxkovzh .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#czajxkovzh .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#czajxkovzh .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#czajxkovzh .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#czajxkovzh .gt_spanner_row {
  border-bottom-style: hidden;
}

#czajxkovzh .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#czajxkovzh .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#czajxkovzh .gt_from_md > :first-child {
  margin-top: 0;
}

#czajxkovzh .gt_from_md > :last-child {
  margin-bottom: 0;
}

#czajxkovzh .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#czajxkovzh .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#czajxkovzh .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#czajxkovzh .gt_row_group_first td {
  border-top-width: 2px;
}

#czajxkovzh .gt_row_group_first th {
  border-top-width: 2px;
}

#czajxkovzh .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#czajxkovzh .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#czajxkovzh .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#czajxkovzh .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#czajxkovzh .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#czajxkovzh .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#czajxkovzh .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#czajxkovzh .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#czajxkovzh .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#czajxkovzh .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#czajxkovzh .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#czajxkovzh .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#czajxkovzh .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#czajxkovzh .gt_left {
  text-align: left;
}

#czajxkovzh .gt_center {
  text-align: center;
}

#czajxkovzh .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#czajxkovzh .gt_font_normal {
  font-weight: normal;
}

#czajxkovzh .gt_font_bold {
  font-weight: bold;
}

#czajxkovzh .gt_font_italic {
  font-style: italic;
}

#czajxkovzh .gt_super {
  font-size: 65%;
}

#czajxkovzh .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#czajxkovzh .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#czajxkovzh .gt_indent_1 {
  text-indent: 5px;
}

#czajxkovzh .gt_indent_2 {
  text-indent: 10px;
}

#czajxkovzh .gt_indent_3 {
  text-indent: 15px;
}

#czajxkovzh .gt_indent_4 {
  text-indent: 20px;
}

#czajxkovzh .gt_indent_5 {
  text-indent: 25px;
}

#czajxkovzh .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#czajxkovzh div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_title gt_font_normal" style>Weekly Growth Snapshot</td>
    </tr>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_subtitle gt_font_normal gt_bottom_border" style>Signups and revenue, last 4 weeks</td>
    </tr>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="signups">signups</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="revenue_usd">revenue_usd</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">W1</th>
<td headers="stub_1_1 signups" class="gt_row gt_right">320</td>
<td headers="stub_1_1 revenue_usd" class="gt_row gt_right">4820</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">W2</th>
<td headers="stub_1_2 signups" class="gt_row gt_right">410</td>
<td headers="stub_1_2 revenue_usd" class="gt_row gt_right">6105</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">W3</th>
<td headers="stub_1_3 signups" class="gt_row gt_right">388</td>
<td headers="stub_1_3 revenue_usd" class="gt_row gt_right">5740</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_left gt_stub">W4</th>
<td headers="stub_1_4 signups" class="gt_row gt_right">502</td>
<td headers="stub_1_4 revenue_usd" class="gt_row gt_right">7888</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Beginner

RYour turn
ex_2_1 <- # your code here ex_2_1

Click to reveal solution
RSolution
ex_2_1 <- ex_1_3 |> tab_header( title = "Weekly Growth Snapshot", subtitle = "Signups and revenue, last 4 weeks" ) ex_2_1 #> Header is two stacked rows above the column labels

Explanation: tab_header() adds a two-tier title block above the column labels: title is bold, subtitle smaller and lighter. Both accept md() or html() helpers if you need bold or links inline. Putting the title in the gt object itself (rather than the report prose) keeps the artifact self-describing when stakeholders screenshot it and paste it into Slack.

Exercise 2.2: Rename columns with cols_label

Task: The finance team finds machine-readable column names like revenue_usd ugly in the boardroom deck. Take ex_2_1 and use cols_label() to relabel signups to "New Signups" and revenue_usd to "Revenue (USD)". Save the table to ex_2_2.

Expected result:

<div id="ddvrmedbxr" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#ddvrmedbxr table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#ddvrmedbxr thead, #ddvrmedbxr tbody, #ddvrmedbxr tfoot, #ddvrmedbxr tr, #ddvrmedbxr td, #ddvrmedbxr th {
  border-style: none;
}

#ddvrmedbxr p {
  margin: 0;
  padding: 0;
}

#ddvrmedbxr .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#ddvrmedbxr .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#ddvrmedbxr .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#ddvrmedbxr .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#ddvrmedbxr .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#ddvrmedbxr .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ddvrmedbxr .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#ddvrmedbxr .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#ddvrmedbxr .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#ddvrmedbxr .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#ddvrmedbxr .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#ddvrmedbxr .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#ddvrmedbxr .gt_spanner_row {
  border-bottom-style: hidden;
}

#ddvrmedbxr .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#ddvrmedbxr .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#ddvrmedbxr .gt_from_md > :first-child {
  margin-top: 0;
}

#ddvrmedbxr .gt_from_md > :last-child {
  margin-bottom: 0;
}

#ddvrmedbxr .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#ddvrmedbxr .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#ddvrmedbxr .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#ddvrmedbxr .gt_row_group_first td {
  border-top-width: 2px;
}

#ddvrmedbxr .gt_row_group_first th {
  border-top-width: 2px;
}

#ddvrmedbxr .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#ddvrmedbxr .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#ddvrmedbxr .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#ddvrmedbxr .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ddvrmedbxr .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#ddvrmedbxr .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#ddvrmedbxr .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#ddvrmedbxr .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#ddvrmedbxr .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ddvrmedbxr .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#ddvrmedbxr .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#ddvrmedbxr .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#ddvrmedbxr .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#ddvrmedbxr .gt_left {
  text-align: left;
}

#ddvrmedbxr .gt_center {
  text-align: center;
}

#ddvrmedbxr .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#ddvrmedbxr .gt_font_normal {
  font-weight: normal;
}

#ddvrmedbxr .gt_font_bold {
  font-weight: bold;
}

#ddvrmedbxr .gt_font_italic {
  font-style: italic;
}

#ddvrmedbxr .gt_super {
  font-size: 65%;
}

#ddvrmedbxr .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#ddvrmedbxr .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#ddvrmedbxr .gt_indent_1 {
  text-indent: 5px;
}

#ddvrmedbxr .gt_indent_2 {
  text-indent: 10px;
}

#ddvrmedbxr .gt_indent_3 {
  text-indent: 15px;
}

#ddvrmedbxr .gt_indent_4 {
  text-indent: 20px;
}

#ddvrmedbxr .gt_indent_5 {
  text-indent: 25px;
}

#ddvrmedbxr .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#ddvrmedbxr div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_title gt_font_normal" style>Weekly Growth Snapshot</td>
    </tr>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_subtitle gt_font_normal gt_bottom_border" style>Signups and revenue, last 4 weeks</td>
    </tr>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="signups">New Signups</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="revenue_usd">Revenue (USD)</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">W1</th>
<td headers="stub_1_1 signups" class="gt_row gt_right">320</td>
<td headers="stub_1_1 revenue_usd" class="gt_row gt_right">4820</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">W2</th>
<td headers="stub_1_2 signups" class="gt_row gt_right">410</td>
<td headers="stub_1_2 revenue_usd" class="gt_row gt_right">6105</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">W3</th>
<td headers="stub_1_3 signups" class="gt_row gt_right">388</td>
<td headers="stub_1_3 revenue_usd" class="gt_row gt_right">5740</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_left gt_stub">W4</th>
<td headers="stub_1_4 signups" class="gt_row gt_right">502</td>
<td headers="stub_1_4 revenue_usd" class="gt_row gt_right">7888</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Beginner

RYour turn
ex_2_2 <- # your code here ex_2_2

Click to reveal solution
RSolution
ex_2_2 <- ex_2_1 |> cols_label( signups = "New Signups", revenue_usd = "Revenue (USD)" ) ex_2_2 #> Column labels now read "New Signups" and "Revenue (USD)"

Explanation: cols_label() changes only the display label, not the underlying column name. This is important because every other gt verb (fmt_number(), tab_style(), summary_rows()) still references the raw column name. Keep the raw names lowercase snake_case in the tibble and treat cols_label() as the presentation layer, the same separation ggplot2::labs() enforces in plots.

Exercise 2.3: Group two columns under a spanner with tab_spanner

Task: The analyst wants signups and revenue grouped visually under a "This week" header. Take ex_2_2 and use tab_spanner(label = "This week", columns = c(signups, revenue_usd)) to draw a single banner above those two columns. Save the result to ex_2_3.

Expected result:

<div id="cxwquzrffb" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#cxwquzrffb table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#cxwquzrffb thead, #cxwquzrffb tbody, #cxwquzrffb tfoot, #cxwquzrffb tr, #cxwquzrffb td, #cxwquzrffb th {
  border-style: none;
}

#cxwquzrffb p {
  margin: 0;
  padding: 0;
}

#cxwquzrffb .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#cxwquzrffb .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#cxwquzrffb .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#cxwquzrffb .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#cxwquzrffb .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#cxwquzrffb .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#cxwquzrffb .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#cxwquzrffb .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#cxwquzrffb .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#cxwquzrffb .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#cxwquzrffb .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#cxwquzrffb .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#cxwquzrffb .gt_spanner_row {
  border-bottom-style: hidden;
}

#cxwquzrffb .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#cxwquzrffb .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#cxwquzrffb .gt_from_md > :first-child {
  margin-top: 0;
}

#cxwquzrffb .gt_from_md > :last-child {
  margin-bottom: 0;
}

#cxwquzrffb .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#cxwquzrffb .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#cxwquzrffb .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#cxwquzrffb .gt_row_group_first td {
  border-top-width: 2px;
}

#cxwquzrffb .gt_row_group_first th {
  border-top-width: 2px;
}

#cxwquzrffb .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#cxwquzrffb .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#cxwquzrffb .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#cxwquzrffb .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#cxwquzrffb .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#cxwquzrffb .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#cxwquzrffb .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#cxwquzrffb .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#cxwquzrffb .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#cxwquzrffb .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#cxwquzrffb .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#cxwquzrffb .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#cxwquzrffb .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#cxwquzrffb .gt_left {
  text-align: left;
}

#cxwquzrffb .gt_center {
  text-align: center;
}

#cxwquzrffb .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#cxwquzrffb .gt_font_normal {
  font-weight: normal;
}

#cxwquzrffb .gt_font_bold {
  font-weight: bold;
}

#cxwquzrffb .gt_font_italic {
  font-style: italic;
}

#cxwquzrffb .gt_super {
  font-size: 65%;
}

#cxwquzrffb .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#cxwquzrffb .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#cxwquzrffb .gt_indent_1 {
  text-indent: 5px;
}

#cxwquzrffb .gt_indent_2 {
  text-indent: 10px;
}

#cxwquzrffb .gt_indent_3 {
  text-indent: 15px;
}

#cxwquzrffb .gt_indent_4 {
  text-indent: 20px;
}

#cxwquzrffb .gt_indent_5 {
  text-indent: 25px;
}

#cxwquzrffb .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#cxwquzrffb div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_title gt_font_normal" style>Weekly Growth Snapshot</td>
    </tr>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_subtitle gt_font_normal gt_bottom_border" style>Signups and revenue, last 4 weeks</td>
    </tr>
    <tr class="gt_col_headings gt_spanner_row">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="2" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_center gt_columns_top_border gt_column_spanner_outer" rowspan="1" colspan="2" scope="colgroup" id="This week">
        <div class="gt_column_spanner">This week</div>
      </th>
    </tr>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="signups">New Signups</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="revenue_usd">Revenue (USD)</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">W1</th>
<td headers="stub_1_1 signups" class="gt_row gt_right">320</td>
<td headers="stub_1_1 revenue_usd" class="gt_row gt_right">4820</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">W2</th>
<td headers="stub_1_2 signups" class="gt_row gt_right">410</td>
<td headers="stub_1_2 revenue_usd" class="gt_row gt_right">6105</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">W3</th>
<td headers="stub_1_3 signups" class="gt_row gt_right">388</td>
<td headers="stub_1_3 revenue_usd" class="gt_row gt_right">5740</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_left gt_stub">W4</th>
<td headers="stub_1_4 signups" class="gt_row gt_right">502</td>
<td headers="stub_1_4 revenue_usd" class="gt_row gt_right">7888</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Intermediate

RYour turn
ex_2_3 <- # your code here ex_2_3

Click to reveal solution
RSolution
ex_2_3 <- ex_2_2 |> tab_spanner( label = "This week", columns = c(signups, revenue_usd) ) ex_2_3 #> Spanner row appears between the header and the column labels

Explanation: Spanners are the gt analogue of merged header cells in Excel. They group columns visually without changing the data shape and are essential when a wide table mixes, say, "Q1" and "Q2" blocks of three columns each. You reference columns with tidy-select (c(...), starts_with("q1_"), where(is.numeric)) inside columns =, exactly like dplyr::select().

Exercise 2.4: Move and hide columns with cols_move and cols_hide

Task: A junior analyst onboarding the deck needs Revenue to appear before Signups, and the index column dropped. Build an inline tibble with idx (1:4), week (W1 to W4), signups (c(320,410,388,502)), revenue_usd (c(4820,6105,5740,7888)), pipe into gt(), then use cols_move(columns = revenue_usd, after = week) and cols_hide(columns = idx). Save to ex_2_4.

Expected result:

<div id="tcvubwfzjh" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#tcvubwfzjh table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#tcvubwfzjh thead, #tcvubwfzjh tbody, #tcvubwfzjh tfoot, #tcvubwfzjh tr, #tcvubwfzjh td, #tcvubwfzjh th {
  border-style: none;
}

#tcvubwfzjh p {
  margin: 0;
  padding: 0;
}

#tcvubwfzjh .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#tcvubwfzjh .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#tcvubwfzjh .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#tcvubwfzjh .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#tcvubwfzjh .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#tcvubwfzjh .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#tcvubwfzjh .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#tcvubwfzjh .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#tcvubwfzjh .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#tcvubwfzjh .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#tcvubwfzjh .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#tcvubwfzjh .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#tcvubwfzjh .gt_spanner_row {
  border-bottom-style: hidden;
}

#tcvubwfzjh .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#tcvubwfzjh .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#tcvubwfzjh .gt_from_md > :first-child {
  margin-top: 0;
}

#tcvubwfzjh .gt_from_md > :last-child {
  margin-bottom: 0;
}

#tcvubwfzjh .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#tcvubwfzjh .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#tcvubwfzjh .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#tcvubwfzjh .gt_row_group_first td {
  border-top-width: 2px;
}

#tcvubwfzjh .gt_row_group_first th {
  border-top-width: 2px;
}

#tcvubwfzjh .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#tcvubwfzjh .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#tcvubwfzjh .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#tcvubwfzjh .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#tcvubwfzjh .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#tcvubwfzjh .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#tcvubwfzjh .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#tcvubwfzjh .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#tcvubwfzjh .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#tcvubwfzjh .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#tcvubwfzjh .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#tcvubwfzjh .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#tcvubwfzjh .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#tcvubwfzjh .gt_left {
  text-align: left;
}

#tcvubwfzjh .gt_center {
  text-align: center;
}

#tcvubwfzjh .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#tcvubwfzjh .gt_font_normal {
  font-weight: normal;
}

#tcvubwfzjh .gt_font_bold {
  font-weight: bold;
}

#tcvubwfzjh .gt_font_italic {
  font-style: italic;
}

#tcvubwfzjh .gt_super {
  font-size: 65%;
}

#tcvubwfzjh .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#tcvubwfzjh .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#tcvubwfzjh .gt_indent_1 {
  text-indent: 5px;
}

#tcvubwfzjh .gt_indent_2 {
  text-indent: 10px;
}

#tcvubwfzjh .gt_indent_3 {
  text-indent: 15px;
}

#tcvubwfzjh .gt_indent_4 {
  text-indent: 20px;
}

#tcvubwfzjh .gt_indent_5 {
  text-indent: 25px;
}

#tcvubwfzjh .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#tcvubwfzjh div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="week">week</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="revenue_usd">revenue_usd</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="signups">signups</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><td headers="week" class="gt_row gt_left">W1</td>
<td headers="revenue_usd" class="gt_row gt_right">4820</td>
<td headers="signups" class="gt_row gt_right">320</td></tr>
    <tr><td headers="week" class="gt_row gt_left">W2</td>
<td headers="revenue_usd" class="gt_row gt_right">6105</td>
<td headers="signups" class="gt_row gt_right">410</td></tr>
    <tr><td headers="week" class="gt_row gt_left">W3</td>
<td headers="revenue_usd" class="gt_row gt_right">5740</td>
<td headers="signups" class="gt_row gt_right">388</td></tr>
    <tr><td headers="week" class="gt_row gt_left">W4</td>
<td headers="revenue_usd" class="gt_row gt_right">7888</td>
<td headers="signups" class="gt_row gt_right">502</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Intermediate

RYour turn
ex_2_4 <- # your code here ex_2_4

Click to reveal solution
RSolution
kpi2 <- tibble( idx = 1:4, week = c("W1", "W2", "W3", "W4"), signups = c(320, 410, 388, 502), revenue_usd = c(4820, 6105, 5740, 7888) ) ex_2_4 <- gt(kpi2) |> cols_move(columns = revenue_usd, after = week) |> cols_hide(columns = idx) ex_2_4 #> Columns shown: week, revenue_usd, signups (idx hidden)

Explanation: cols_move() and cols_hide() re-order and suppress columns at the presentation layer without mutating the data, so you can pass a single canonical tibble to gt() and customise every output for its audience. Compare with select() upstream: cols_hide() keeps the data accessible for summary_rows() or tab_footnote() references, while select() removes it from gt entirely.

Section 3. Formatting numbers, currency, and dates (4 problems)

Exercise 3.1: Format a numeric column with fmt_number

Task: The finance team complains the revenue column shows 4820 instead of 4,820. Take ex_2_3 and apply fmt_number(columns = revenue_usd, decimals = 0, use_seps = TRUE) to insert thousands separators. Save to ex_3_1.

Expected result:

<div id="eaqgdmjilt" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#eaqgdmjilt table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#eaqgdmjilt thead, #eaqgdmjilt tbody, #eaqgdmjilt tfoot, #eaqgdmjilt tr, #eaqgdmjilt td, #eaqgdmjilt th {
  border-style: none;
}

#eaqgdmjilt p {
  margin: 0;
  padding: 0;
}

#eaqgdmjilt .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#eaqgdmjilt .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#eaqgdmjilt .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#eaqgdmjilt .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#eaqgdmjilt .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#eaqgdmjilt .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#eaqgdmjilt .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#eaqgdmjilt .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#eaqgdmjilt .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#eaqgdmjilt .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#eaqgdmjilt .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#eaqgdmjilt .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#eaqgdmjilt .gt_spanner_row {
  border-bottom-style: hidden;
}

#eaqgdmjilt .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#eaqgdmjilt .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#eaqgdmjilt .gt_from_md > :first-child {
  margin-top: 0;
}

#eaqgdmjilt .gt_from_md > :last-child {
  margin-bottom: 0;
}

#eaqgdmjilt .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#eaqgdmjilt .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#eaqgdmjilt .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#eaqgdmjilt .gt_row_group_first td {
  border-top-width: 2px;
}

#eaqgdmjilt .gt_row_group_first th {
  border-top-width: 2px;
}

#eaqgdmjilt .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#eaqgdmjilt .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#eaqgdmjilt .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#eaqgdmjilt .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#eaqgdmjilt .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#eaqgdmjilt .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#eaqgdmjilt .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#eaqgdmjilt .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#eaqgdmjilt .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#eaqgdmjilt .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#eaqgdmjilt .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#eaqgdmjilt .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#eaqgdmjilt .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#eaqgdmjilt .gt_left {
  text-align: left;
}

#eaqgdmjilt .gt_center {
  text-align: center;
}

#eaqgdmjilt .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#eaqgdmjilt .gt_font_normal {
  font-weight: normal;
}

#eaqgdmjilt .gt_font_bold {
  font-weight: bold;
}

#eaqgdmjilt .gt_font_italic {
  font-style: italic;
}

#eaqgdmjilt .gt_super {
  font-size: 65%;
}

#eaqgdmjilt .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#eaqgdmjilt .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#eaqgdmjilt .gt_indent_1 {
  text-indent: 5px;
}

#eaqgdmjilt .gt_indent_2 {
  text-indent: 10px;
}

#eaqgdmjilt .gt_indent_3 {
  text-indent: 15px;
}

#eaqgdmjilt .gt_indent_4 {
  text-indent: 20px;
}

#eaqgdmjilt .gt_indent_5 {
  text-indent: 25px;
}

#eaqgdmjilt .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#eaqgdmjilt div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_title gt_font_normal" style>Weekly Growth Snapshot</td>
    </tr>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_subtitle gt_font_normal gt_bottom_border" style>Signups and revenue, last 4 weeks</td>
    </tr>
    <tr class="gt_col_headings gt_spanner_row">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="2" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_center gt_columns_top_border gt_column_spanner_outer" rowspan="1" colspan="2" scope="colgroup" id="This week">
        <div class="gt_column_spanner">This week</div>
      </th>
    </tr>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="signups">New Signups</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="revenue_usd">Revenue (USD)</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">W1</th>
<td headers="stub_1_1 signups" class="gt_row gt_right">320</td>
<td headers="stub_1_1 revenue_usd" class="gt_row gt_right">4,820</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">W2</th>
<td headers="stub_1_2 signups" class="gt_row gt_right">410</td>
<td headers="stub_1_2 revenue_usd" class="gt_row gt_right">6,105</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">W3</th>
<td headers="stub_1_3 signups" class="gt_row gt_right">388</td>
<td headers="stub_1_3 revenue_usd" class="gt_row gt_right">5,740</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_left gt_stub">W4</th>
<td headers="stub_1_4 signups" class="gt_row gt_right">502</td>
<td headers="stub_1_4 revenue_usd" class="gt_row gt_right">7,888</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Intermediate

RYour turn
ex_3_1 <- # your code here ex_3_1

Click to reveal solution
RSolution
ex_3_1 <- ex_2_3 |> fmt_number( columns = revenue_usd, decimals = 0, use_seps = TRUE ) ex_3_1 #> revenue_usd values render with commas: 4,820, 6,105, 5,740, 7,888

Explanation: fmt_number() is the workhorse formatter. decimals = 0 rounds for display only, the underlying value is preserved for sorting and summaries. use_seps = TRUE honours the system locale, so en_US gives commas and de_DE gives points; pass sep_mark = "," to override. Format only the columns that need it: applying number formatting to ID columns is a frequent rookie mistake that turns "1001" into "1,001".

Exercise 3.2: Apply fmt_currency to a price column

Task: A retailer is preparing a board pack. Build an inline tibble with sku (A001, A002, A003), units (c(120, 84, 200)), and price (c(19.99, 49.5, 8.25)), pipe into gt(rowname_col = "sku"), then format price as USD with two decimals using fmt_currency(columns = price, currency = "USD"). Save to ex_3_2.

Expected result:

<div id="ikcpexnevb" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#ikcpexnevb table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#ikcpexnevb thead, #ikcpexnevb tbody, #ikcpexnevb tfoot, #ikcpexnevb tr, #ikcpexnevb td, #ikcpexnevb th {
  border-style: none;
}

#ikcpexnevb p {
  margin: 0;
  padding: 0;
}

#ikcpexnevb .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#ikcpexnevb .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#ikcpexnevb .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#ikcpexnevb .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#ikcpexnevb .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#ikcpexnevb .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ikcpexnevb .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#ikcpexnevb .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#ikcpexnevb .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#ikcpexnevb .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#ikcpexnevb .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#ikcpexnevb .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#ikcpexnevb .gt_spanner_row {
  border-bottom-style: hidden;
}

#ikcpexnevb .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#ikcpexnevb .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#ikcpexnevb .gt_from_md > :first-child {
  margin-top: 0;
}

#ikcpexnevb .gt_from_md > :last-child {
  margin-bottom: 0;
}

#ikcpexnevb .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#ikcpexnevb .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#ikcpexnevb .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#ikcpexnevb .gt_row_group_first td {
  border-top-width: 2px;
}

#ikcpexnevb .gt_row_group_first th {
  border-top-width: 2px;
}

#ikcpexnevb .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#ikcpexnevb .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#ikcpexnevb .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#ikcpexnevb .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ikcpexnevb .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#ikcpexnevb .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#ikcpexnevb .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#ikcpexnevb .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#ikcpexnevb .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ikcpexnevb .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#ikcpexnevb .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#ikcpexnevb .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#ikcpexnevb .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#ikcpexnevb .gt_left {
  text-align: left;
}

#ikcpexnevb .gt_center {
  text-align: center;
}

#ikcpexnevb .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#ikcpexnevb .gt_font_normal {
  font-weight: normal;
}

#ikcpexnevb .gt_font_bold {
  font-weight: bold;
}

#ikcpexnevb .gt_font_italic {
  font-style: italic;
}

#ikcpexnevb .gt_super {
  font-size: 65%;
}

#ikcpexnevb .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#ikcpexnevb .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#ikcpexnevb .gt_indent_1 {
  text-indent: 5px;
}

#ikcpexnevb .gt_indent_2 {
  text-indent: 10px;
}

#ikcpexnevb .gt_indent_3 {
  text-indent: 15px;
}

#ikcpexnevb .gt_indent_4 {
  text-indent: 20px;
}

#ikcpexnevb .gt_indent_5 {
  text-indent: 25px;
}

#ikcpexnevb .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#ikcpexnevb div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="units">units</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="price">price</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">A001</th>
<td headers="stub_1_1 units" class="gt_row gt_right">120</td>
<td headers="stub_1_1 price" class="gt_row gt_right">$19.99</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">A002</th>
<td headers="stub_1_2 units" class="gt_row gt_right">84</td>
<td headers="stub_1_2 price" class="gt_row gt_right">$49.50</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">A003</th>
<td headers="stub_1_3 units" class="gt_row gt_right">200</td>
<td headers="stub_1_3 price" class="gt_row gt_right">$8.25</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Intermediate

RYour turn
ex_3_2 <- # your code here ex_3_2

Click to reveal solution
RSolution
prices <- tibble( sku = c("A001", "A002", "A003"), units = c(120, 84, 200), price = c(19.99, 49.50, 8.25) ) ex_3_2 <- gt(prices, rowname_col = "sku") |> fmt_currency(columns = price, currency = "USD") ex_3_2 #> price column shows $19.99, $49.50, $8.25

Explanation: fmt_currency() is fmt_number() plus a currency glyph and ISO-aware decimal rules (most currencies default to 2 decimals, JPY to 0). Pass currency = "EUR" for Euros, currency = "GBP" for pounds, etc. For multi-currency reports, format each row group separately using the rows = argument or use fmt() with a custom function.

Exercise 3.3: Show percentages with fmt_percent

Task: The performance reviewer needs conversion rates expressed as percentages. Build an inline tibble with channel (Email, Paid, Organic), clicks (c(8200, 12400, 4150)), and cvr (c(0.042, 0.018, 0.071) which are raw proportions), then build a gt table with stub=channel and format cvr to display as percent with 1 decimal using fmt_percent(columns = cvr, decimals = 1). Save to ex_3_3.

Expected result:

<div id="rxqrenhwhu" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#rxqrenhwhu table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#rxqrenhwhu thead, #rxqrenhwhu tbody, #rxqrenhwhu tfoot, #rxqrenhwhu tr, #rxqrenhwhu td, #rxqrenhwhu th {
  border-style: none;
}

#rxqrenhwhu p {
  margin: 0;
  padding: 0;
}

#rxqrenhwhu .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#rxqrenhwhu .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#rxqrenhwhu .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#rxqrenhwhu .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#rxqrenhwhu .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#rxqrenhwhu .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#rxqrenhwhu .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#rxqrenhwhu .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#rxqrenhwhu .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#rxqrenhwhu .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#rxqrenhwhu .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#rxqrenhwhu .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#rxqrenhwhu .gt_spanner_row {
  border-bottom-style: hidden;
}

#rxqrenhwhu .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#rxqrenhwhu .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#rxqrenhwhu .gt_from_md > :first-child {
  margin-top: 0;
}

#rxqrenhwhu .gt_from_md > :last-child {
  margin-bottom: 0;
}

#rxqrenhwhu .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#rxqrenhwhu .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#rxqrenhwhu .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#rxqrenhwhu .gt_row_group_first td {
  border-top-width: 2px;
}

#rxqrenhwhu .gt_row_group_first th {
  border-top-width: 2px;
}

#rxqrenhwhu .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#rxqrenhwhu .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#rxqrenhwhu .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#rxqrenhwhu .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#rxqrenhwhu .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#rxqrenhwhu .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#rxqrenhwhu .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#rxqrenhwhu .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#rxqrenhwhu .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#rxqrenhwhu .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#rxqrenhwhu .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#rxqrenhwhu .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#rxqrenhwhu .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#rxqrenhwhu .gt_left {
  text-align: left;
}

#rxqrenhwhu .gt_center {
  text-align: center;
}

#rxqrenhwhu .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#rxqrenhwhu .gt_font_normal {
  font-weight: normal;
}

#rxqrenhwhu .gt_font_bold {
  font-weight: bold;
}

#rxqrenhwhu .gt_font_italic {
  font-style: italic;
}

#rxqrenhwhu .gt_super {
  font-size: 65%;
}

#rxqrenhwhu .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#rxqrenhwhu .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#rxqrenhwhu .gt_indent_1 {
  text-indent: 5px;
}

#rxqrenhwhu .gt_indent_2 {
  text-indent: 10px;
}

#rxqrenhwhu .gt_indent_3 {
  text-indent: 15px;
}

#rxqrenhwhu .gt_indent_4 {
  text-indent: 20px;
}

#rxqrenhwhu .gt_indent_5 {
  text-indent: 25px;
}

#rxqrenhwhu .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#rxqrenhwhu div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="clicks">clicks</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="cvr">cvr</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">Email</th>
<td headers="stub_1_1 clicks" class="gt_row gt_right">8200</td>
<td headers="stub_1_1 cvr" class="gt_row gt_right">4.2%</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">Paid</th>
<td headers="stub_1_2 clicks" class="gt_row gt_right">12400</td>
<td headers="stub_1_2 cvr" class="gt_row gt_right">1.8%</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">Organic</th>
<td headers="stub_1_3 clicks" class="gt_row gt_right">4150</td>
<td headers="stub_1_3 cvr" class="gt_row gt_right">7.1%</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Intermediate

RYour turn
ex_3_3 <- # your code here ex_3_3

Click to reveal solution
RSolution
conv <- tibble( channel = c("Email", "Paid", "Organic"), clicks = c(8200, 12400, 4150), cvr = c(0.042, 0.018, 0.071) ) ex_3_3 <- gt(conv, rowname_col = "channel") |> fmt_percent(columns = cvr, decimals = 1) ex_3_3 #> cvr displayed as 4.2%, 1.8%, 7.1%

Explanation: fmt_percent() assumes the underlying value is a proportion in [0, 1] and multiplies by 100 for display. If your data already stores percentages (4.2 instead of 0.042) pass scale_values = FALSE to suppress the multiplication. Mixing pre-scaled and proportion data inside one column is the single most common source of "my conversion rate is 4200%" bugs, audit the source once and stick to one convention.

Exercise 3.4: Format a Date column with fmt_date

Task: Build an inline tibble of release dates with release (the dates 2025-01-15, 2025-03-08, 2025-06-22 as a Date vector via as.Date()) and version (c("v1.0", "v1.1", "v2.0")). Build a gt table and use fmt_date(columns = release, date_style = "month_day_year") to display the date as "January 15, 2025" rather than ISO. Save to ex_3_4.

Expected result:

<div id="dydgjrmuvs" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#dydgjrmuvs table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#dydgjrmuvs thead, #dydgjrmuvs tbody, #dydgjrmuvs tfoot, #dydgjrmuvs tr, #dydgjrmuvs td, #dydgjrmuvs th {
  border-style: none;
}

#dydgjrmuvs p {
  margin: 0;
  padding: 0;
}

#dydgjrmuvs .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#dydgjrmuvs .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#dydgjrmuvs .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#dydgjrmuvs .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#dydgjrmuvs .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#dydgjrmuvs .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#dydgjrmuvs .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#dydgjrmuvs .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#dydgjrmuvs .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#dydgjrmuvs .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#dydgjrmuvs .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#dydgjrmuvs .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#dydgjrmuvs .gt_spanner_row {
  border-bottom-style: hidden;
}

#dydgjrmuvs .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#dydgjrmuvs .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#dydgjrmuvs .gt_from_md > :first-child {
  margin-top: 0;
}

#dydgjrmuvs .gt_from_md > :last-child {
  margin-bottom: 0;
}

#dydgjrmuvs .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#dydgjrmuvs .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#dydgjrmuvs .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#dydgjrmuvs .gt_row_group_first td {
  border-top-width: 2px;
}

#dydgjrmuvs .gt_row_group_first th {
  border-top-width: 2px;
}

#dydgjrmuvs .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#dydgjrmuvs .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#dydgjrmuvs .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#dydgjrmuvs .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#dydgjrmuvs .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#dydgjrmuvs .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#dydgjrmuvs .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#dydgjrmuvs .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#dydgjrmuvs .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#dydgjrmuvs .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#dydgjrmuvs .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#dydgjrmuvs .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#dydgjrmuvs .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#dydgjrmuvs .gt_left {
  text-align: left;
}

#dydgjrmuvs .gt_center {
  text-align: center;
}

#dydgjrmuvs .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#dydgjrmuvs .gt_font_normal {
  font-weight: normal;
}

#dydgjrmuvs .gt_font_bold {
  font-weight: bold;
}

#dydgjrmuvs .gt_font_italic {
  font-style: italic;
}

#dydgjrmuvs .gt_super {
  font-size: 65%;
}

#dydgjrmuvs .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#dydgjrmuvs .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#dydgjrmuvs .gt_indent_1 {
  text-indent: 5px;
}

#dydgjrmuvs .gt_indent_2 {
  text-indent: 10px;
}

#dydgjrmuvs .gt_indent_3 {
  text-indent: 15px;
}

#dydgjrmuvs .gt_indent_4 {
  text-indent: 20px;
}

#dydgjrmuvs .gt_indent_5 {
  text-indent: 25px;
}

#dydgjrmuvs .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#dydgjrmuvs div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="release">release</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="version">version</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><td headers="release" class="gt_row gt_right">January 15, 2025</td>
<td headers="version" class="gt_row gt_left">v1.0</td></tr>
    <tr><td headers="release" class="gt_row gt_right">March 8, 2025</td>
<td headers="version" class="gt_row gt_left">v1.1</td></tr>
    <tr><td headers="release" class="gt_row gt_right">June 22, 2025</td>
<td headers="version" class="gt_row gt_left">v2.0</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Intermediate

RYour turn
ex_3_4 <- # your code here ex_3_4

Click to reveal solution
RSolution
releases <- tibble( release = as.Date(c("2025-01-15", "2025-03-08", "2025-06-22")), version = c("v1.0", "v1.1", "v2.0") ) ex_3_4 <- gt(releases) |> fmt_date(columns = release, date_style = "month_day_year") ex_3_4 #> release column: January 15, 2025 / March 8, 2025 / June 22, 2025

Explanation: fmt_date() takes a date_style argument with named conventions like "iso", "month_day_year", "day_m_year", "yMMMd". Run info_date_style() to see the full table inside R. The column must already be a Date or POSIXt, gt does not parse strings, so as.Date() upstream is required. For datetimes with hour and minute use fmt_datetime() instead.

Section 4. Styling cells and conditional formatting (4 problems)

Exercise 4.1: Bold the column labels with tab_style

Task: The performance reviewer asks for stronger emphasis on column labels in the slide deck. Take ex_3_1 and use tab_style(style = cell_text(weight = "bold"), locations = cells_column_labels()) to make every column label bold. Save the result to ex_4_1.

Expected result:

<div id="pvqliyukyz" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#pvqliyukyz table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#pvqliyukyz thead, #pvqliyukyz tbody, #pvqliyukyz tfoot, #pvqliyukyz tr, #pvqliyukyz td, #pvqliyukyz th {
  border-style: none;
}

#pvqliyukyz p {
  margin: 0;
  padding: 0;
}

#pvqliyukyz .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#pvqliyukyz .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#pvqliyukyz .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#pvqliyukyz .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#pvqliyukyz .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#pvqliyukyz .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#pvqliyukyz .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#pvqliyukyz .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#pvqliyukyz .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#pvqliyukyz .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#pvqliyukyz .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#pvqliyukyz .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#pvqliyukyz .gt_spanner_row {
  border-bottom-style: hidden;
}

#pvqliyukyz .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#pvqliyukyz .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#pvqliyukyz .gt_from_md > :first-child {
  margin-top: 0;
}

#pvqliyukyz .gt_from_md > :last-child {
  margin-bottom: 0;
}

#pvqliyukyz .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#pvqliyukyz .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#pvqliyukyz .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#pvqliyukyz .gt_row_group_first td {
  border-top-width: 2px;
}

#pvqliyukyz .gt_row_group_first th {
  border-top-width: 2px;
}

#pvqliyukyz .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#pvqliyukyz .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#pvqliyukyz .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#pvqliyukyz .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#pvqliyukyz .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#pvqliyukyz .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#pvqliyukyz .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#pvqliyukyz .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#pvqliyukyz .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#pvqliyukyz .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#pvqliyukyz .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#pvqliyukyz .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#pvqliyukyz .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#pvqliyukyz .gt_left {
  text-align: left;
}

#pvqliyukyz .gt_center {
  text-align: center;
}

#pvqliyukyz .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#pvqliyukyz .gt_font_normal {
  font-weight: normal;
}

#pvqliyukyz .gt_font_bold {
  font-weight: bold;
}

#pvqliyukyz .gt_font_italic {
  font-style: italic;
}

#pvqliyukyz .gt_super {
  font-size: 65%;
}

#pvqliyukyz .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#pvqliyukyz .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#pvqliyukyz .gt_indent_1 {
  text-indent: 5px;
}

#pvqliyukyz .gt_indent_2 {
  text-indent: 10px;
}

#pvqliyukyz .gt_indent_3 {
  text-indent: 15px;
}

#pvqliyukyz .gt_indent_4 {
  text-indent: 20px;
}

#pvqliyukyz .gt_indent_5 {
  text-indent: 25px;
}

#pvqliyukyz .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#pvqliyukyz div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_title gt_font_normal" style>Weekly Growth Snapshot</td>
    </tr>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_subtitle gt_font_normal gt_bottom_border" style>Signups and revenue, last 4 weeks</td>
    </tr>
    <tr class="gt_col_headings gt_spanner_row">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="2" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_center gt_columns_top_border gt_column_spanner_outer" rowspan="1" colspan="2" scope="colgroup" id="This week">
        <div class="gt_column_spanner">This week</div>
      </th>
    </tr>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" style="font-weight: bold;" scope="col" id="signups">New Signups</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" style="font-weight: bold;" scope="col" id="revenue_usd">Revenue (USD)</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">W1</th>
<td headers="stub_1_1 signups" class="gt_row gt_right">320</td>
<td headers="stub_1_1 revenue_usd" class="gt_row gt_right">4,820</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">W2</th>
<td headers="stub_1_2 signups" class="gt_row gt_right">410</td>
<td headers="stub_1_2 revenue_usd" class="gt_row gt_right">6,105</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">W3</th>
<td headers="stub_1_3 signups" class="gt_row gt_right">388</td>
<td headers="stub_1_3 revenue_usd" class="gt_row gt_right">5,740</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_left gt_stub">W4</th>
<td headers="stub_1_4 signups" class="gt_row gt_right">502</td>
<td headers="stub_1_4 revenue_usd" class="gt_row gt_right">7,888</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Intermediate

RYour turn
ex_4_1 <- # your code here ex_4_1

Click to reveal solution
RSolution
ex_4_1 <- ex_3_1 |> tab_style( style = cell_text(weight = "bold"), locations = cells_column_labels() ) ex_4_1 #> Column labels rendered with font-weight: bold

Explanation: tab_style() is gt's universal styling primitive: a style = argument (built from cell_text(), cell_fill(), cell_borders()) plus a locations = argument (one of cells_body(), cells_column_labels(), cells_stub(), cells_title(), cells_row_groups(), etc.). The split lets you describe what you want and where it goes orthogonally, which is why gt scales to dense, multi-block tables without spaghetti styling code.

Exercise 4.2: Highlight a single cell with cell_fill

Task: The growth team wants the best revenue week called out. Take ex_4_1 and use tab_style(style = cell_fill(color = "#FFF59D"), locations = cells_body(columns = revenue_usd, rows = revenue_usd == max(revenue_usd))) to paint the maximum revenue cell pale yellow. The underlying data is the kpi tibble from earlier; the max is in W4. Save to ex_4_2.

Expected result:

<div id="phwfamneiw" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#phwfamneiw table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#phwfamneiw thead, #phwfamneiw tbody, #phwfamneiw tfoot, #phwfamneiw tr, #phwfamneiw td, #phwfamneiw th {
  border-style: none;
}

#phwfamneiw p {
  margin: 0;
  padding: 0;
}

#phwfamneiw .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#phwfamneiw .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#phwfamneiw .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#phwfamneiw .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#phwfamneiw .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#phwfamneiw .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#phwfamneiw .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#phwfamneiw .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#phwfamneiw .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#phwfamneiw .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#phwfamneiw .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#phwfamneiw .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#phwfamneiw .gt_spanner_row {
  border-bottom-style: hidden;
}

#phwfamneiw .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#phwfamneiw .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#phwfamneiw .gt_from_md > :first-child {
  margin-top: 0;
}

#phwfamneiw .gt_from_md > :last-child {
  margin-bottom: 0;
}

#phwfamneiw .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#phwfamneiw .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#phwfamneiw .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#phwfamneiw .gt_row_group_first td {
  border-top-width: 2px;
}

#phwfamneiw .gt_row_group_first th {
  border-top-width: 2px;
}

#phwfamneiw .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#phwfamneiw .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#phwfamneiw .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#phwfamneiw .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#phwfamneiw .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#phwfamneiw .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#phwfamneiw .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#phwfamneiw .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#phwfamneiw .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#phwfamneiw .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#phwfamneiw .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#phwfamneiw .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#phwfamneiw .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#phwfamneiw .gt_left {
  text-align: left;
}

#phwfamneiw .gt_center {
  text-align: center;
}

#phwfamneiw .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#phwfamneiw .gt_font_normal {
  font-weight: normal;
}

#phwfamneiw .gt_font_bold {
  font-weight: bold;
}

#phwfamneiw .gt_font_italic {
  font-style: italic;
}

#phwfamneiw .gt_super {
  font-size: 65%;
}

#phwfamneiw .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#phwfamneiw .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#phwfamneiw .gt_indent_1 {
  text-indent: 5px;
}

#phwfamneiw .gt_indent_2 {
  text-indent: 10px;
}

#phwfamneiw .gt_indent_3 {
  text-indent: 15px;
}

#phwfamneiw .gt_indent_4 {
  text-indent: 20px;
}

#phwfamneiw .gt_indent_5 {
  text-indent: 25px;
}

#phwfamneiw .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#phwfamneiw div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_title gt_font_normal" style>Weekly Growth Snapshot</td>
    </tr>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_subtitle gt_font_normal gt_bottom_border" style>Signups and revenue, last 4 weeks</td>
    </tr>
    <tr class="gt_col_headings gt_spanner_row">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="2" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_center gt_columns_top_border gt_column_spanner_outer" rowspan="1" colspan="2" scope="colgroup" id="This week">
        <div class="gt_column_spanner">This week</div>
      </th>
    </tr>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" style="font-weight: bold;" scope="col" id="signups">New Signups</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" style="font-weight: bold;" scope="col" id="revenue_usd">Revenue (USD)</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">W1</th>
<td headers="stub_1_1 signups" class="gt_row gt_right">320</td>
<td headers="stub_1_1 revenue_usd" class="gt_row gt_right">4,820</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">W2</th>
<td headers="stub_1_2 signups" class="gt_row gt_right">410</td>
<td headers="stub_1_2 revenue_usd" class="gt_row gt_right">6,105</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">W3</th>
<td headers="stub_1_3 signups" class="gt_row gt_right">388</td>
<td headers="stub_1_3 revenue_usd" class="gt_row gt_right">5,740</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_left gt_stub">W4</th>
<td headers="stub_1_4 signups" class="gt_row gt_right">502</td>
<td headers="stub_1_4 revenue_usd" class="gt_row gt_right" style="background-color: #FFF59D;">7,888</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Advanced

RYour turn
ex_4_2 <- # your code here ex_4_2

Click to reveal solution
RSolution
ex_4_2 <- ex_4_1 |> tab_style( style = cell_fill(color = "#FFF59D"), locations = cells_body( columns = revenue_usd, rows = revenue_usd == max(revenue_usd) ) ) ex_4_2 #> The W4 revenue cell is highlighted yellow

Explanation: cells_body() accepts rows = as a tidy-eval expression evaluated against the original data, so revenue_usd == max(revenue_usd) resolves to a logical mask over rows. This is how gt does conditional formatting without a loop: declare the predicate and gt picks the right cells. The same expression works inside tab_footnote() or summary_rows() for matched annotations.

Exercise 4.3: Colour-scale a numeric column with data_color

Task: The growth team wants a heatmap effect on the signups column to spot peaks at a glance. Take ex_4_2 and apply data_color(columns = signups, palette = c("white", "#1B9E77")) to fill each cell with a colour proportional to its value (white = low, green = high). Save to ex_4_3.

Expected result:

<div id="pzkjjxayys" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#pzkjjxayys table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#pzkjjxayys thead, #pzkjjxayys tbody, #pzkjjxayys tfoot, #pzkjjxayys tr, #pzkjjxayys td, #pzkjjxayys th {
  border-style: none;
}

#pzkjjxayys p {
  margin: 0;
  padding: 0;
}

#pzkjjxayys .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#pzkjjxayys .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#pzkjjxayys .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#pzkjjxayys .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#pzkjjxayys .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#pzkjjxayys .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#pzkjjxayys .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#pzkjjxayys .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#pzkjjxayys .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#pzkjjxayys .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#pzkjjxayys .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#pzkjjxayys .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#pzkjjxayys .gt_spanner_row {
  border-bottom-style: hidden;
}

#pzkjjxayys .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#pzkjjxayys .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#pzkjjxayys .gt_from_md > :first-child {
  margin-top: 0;
}

#pzkjjxayys .gt_from_md > :last-child {
  margin-bottom: 0;
}

#pzkjjxayys .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#pzkjjxayys .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#pzkjjxayys .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#pzkjjxayys .gt_row_group_first td {
  border-top-width: 2px;
}

#pzkjjxayys .gt_row_group_first th {
  border-top-width: 2px;
}

#pzkjjxayys .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#pzkjjxayys .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#pzkjjxayys .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#pzkjjxayys .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#pzkjjxayys .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#pzkjjxayys .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#pzkjjxayys .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#pzkjjxayys .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#pzkjjxayys .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#pzkjjxayys .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#pzkjjxayys .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#pzkjjxayys .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#pzkjjxayys .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#pzkjjxayys .gt_left {
  text-align: left;
}

#pzkjjxayys .gt_center {
  text-align: center;
}

#pzkjjxayys .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#pzkjjxayys .gt_font_normal {
  font-weight: normal;
}

#pzkjjxayys .gt_font_bold {
  font-weight: bold;
}

#pzkjjxayys .gt_font_italic {
  font-style: italic;
}

#pzkjjxayys .gt_super {
  font-size: 65%;
}

#pzkjjxayys .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#pzkjjxayys .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#pzkjjxayys .gt_indent_1 {
  text-indent: 5px;
}

#pzkjjxayys .gt_indent_2 {
  text-indent: 10px;
}

#pzkjjxayys .gt_indent_3 {
  text-indent: 15px;
}

#pzkjjxayys .gt_indent_4 {
  text-indent: 20px;
}

#pzkjjxayys .gt_indent_5 {
  text-indent: 25px;
}

#pzkjjxayys .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#pzkjjxayys div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_title gt_font_normal" style>Weekly Growth Snapshot</td>
    </tr>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_subtitle gt_font_normal gt_bottom_border" style>Signups and revenue, last 4 weeks</td>
    </tr>
    <tr class="gt_col_headings gt_spanner_row">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="2" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_center gt_columns_top_border gt_column_spanner_outer" rowspan="1" colspan="2" scope="colgroup" id="This week">
        <div class="gt_column_spanner">This week</div>
      </th>
    </tr>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" style="font-weight: bold;" scope="col" id="signups">New Signups</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" style="font-weight: bold;" scope="col" id="revenue_usd">Revenue (USD)</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">W1</th>
<td headers="stub_1_1 signups" class="gt_row gt_right" style="background-color: #FFFFFF; color: #000000;">320</td>
<td headers="stub_1_1 revenue_usd" class="gt_row gt_right">4,820</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">W2</th>
<td headers="stub_1_2 signups" class="gt_row gt_right" style="background-color: #9CCFBA; color: #000000;">410</td>
<td headers="stub_1_2 revenue_usd" class="gt_row gt_right">6,105</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">W3</th>
<td headers="stub_1_3 signups" class="gt_row gt_right" style="background-color: #B4DBCA; color: #000000;">388</td>
<td headers="stub_1_3 revenue_usd" class="gt_row gt_right">5,740</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_left gt_stub">W4</th>
<td headers="stub_1_4 signups" class="gt_row gt_right" style="background-color: #1B9E77; color: #FFFFFF;">502</td>
<td headers="stub_1_4 revenue_usd" class="gt_row gt_right" style="background-color: #FFF59D;">7,888</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Advanced

RYour turn
ex_4_3 <- # your code here ex_4_3

Click to reveal solution
RSolution
ex_4_3 <- ex_4_2 |> data_color( columns = signups, palette = c("white", "#1B9E77") ) ex_4_3 #> signups column gets a continuous colour scale

Explanation: data_color() linearly maps the column's numeric range onto the palette and applies cell_fill() per row. Pass a two-colour vector for a simple gradient, or a named viridis palette via palette = "viridis". For divergent data (gains vs losses) supply three colours and centre the scale with domain = c(-x, 0, x). It is the lazy analyst's heatmap, no manual breaks required.

Exercise 4.4: Conditional text colour with tab_style + locations on rows

Task: A junior analyst is preparing a P&L recap. Build an inline tibble with month (Jan, Feb, Mar, Apr) and pnl (c(-1200, 800, -340, 1500)), gt it with stub=month, and use tab_style() so cells where pnl < 0 render in red and cells where pnl >= 0 render in dark green. Save to ex_4_4.

Expected result:

<div id="qnnofxokep" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#qnnofxokep table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#qnnofxokep thead, #qnnofxokep tbody, #qnnofxokep tfoot, #qnnofxokep tr, #qnnofxokep td, #qnnofxokep th {
  border-style: none;
}

#qnnofxokep p {
  margin: 0;
  padding: 0;
}

#qnnofxokep .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#qnnofxokep .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#qnnofxokep .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#qnnofxokep .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#qnnofxokep .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#qnnofxokep .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qnnofxokep .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#qnnofxokep .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#qnnofxokep .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#qnnofxokep .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#qnnofxokep .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#qnnofxokep .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#qnnofxokep .gt_spanner_row {
  border-bottom-style: hidden;
}

#qnnofxokep .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#qnnofxokep .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#qnnofxokep .gt_from_md > :first-child {
  margin-top: 0;
}

#qnnofxokep .gt_from_md > :last-child {
  margin-bottom: 0;
}

#qnnofxokep .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#qnnofxokep .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#qnnofxokep .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#qnnofxokep .gt_row_group_first td {
  border-top-width: 2px;
}

#qnnofxokep .gt_row_group_first th {
  border-top-width: 2px;
}

#qnnofxokep .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#qnnofxokep .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#qnnofxokep .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#qnnofxokep .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qnnofxokep .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#qnnofxokep .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#qnnofxokep .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#qnnofxokep .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#qnnofxokep .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qnnofxokep .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#qnnofxokep .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#qnnofxokep .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#qnnofxokep .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#qnnofxokep .gt_left {
  text-align: left;
}

#qnnofxokep .gt_center {
  text-align: center;
}

#qnnofxokep .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#qnnofxokep .gt_font_normal {
  font-weight: normal;
}

#qnnofxokep .gt_font_bold {
  font-weight: bold;
}

#qnnofxokep .gt_font_italic {
  font-style: italic;
}

#qnnofxokep .gt_super {
  font-size: 65%;
}

#qnnofxokep .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#qnnofxokep .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#qnnofxokep .gt_indent_1 {
  text-indent: 5px;
}

#qnnofxokep .gt_indent_2 {
  text-indent: 10px;
}

#qnnofxokep .gt_indent_3 {
  text-indent: 15px;
}

#qnnofxokep .gt_indent_4 {
  text-indent: 20px;
}

#qnnofxokep .gt_indent_5 {
  text-indent: 25px;
}

#qnnofxokep .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#qnnofxokep div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="pnl">pnl</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">Jan</th>
<td headers="stub_1_1 pnl" class="gt_row gt_right" style="color: #FF0000;">-1200</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">Feb</th>
<td headers="stub_1_2 pnl" class="gt_row gt_right" style="color: #1B5E20;">800</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">Mar</th>
<td headers="stub_1_3 pnl" class="gt_row gt_right" style="color: #FF0000;">-340</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_left gt_stub">Apr</th>
<td headers="stub_1_4 pnl" class="gt_row gt_right" style="color: #1B5E20;">1500</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Advanced

RYour turn
ex_4_4 <- # your code here ex_4_4

Click to reveal solution
RSolution
pnl <- tibble( month = c("Jan", "Feb", "Mar", "Apr"), pnl = c(-1200, 800, -340, 1500) ) ex_4_4 <- gt(pnl, rowname_col = "month") |> tab_style( style = cell_text(color = "red"), locations = cells_body(columns = pnl, rows = pnl < 0) ) |> tab_style( style = cell_text(color = "#1B5E20"), locations = cells_body(columns = pnl, rows = pnl >= 0) ) ex_4_4 #> Negative values red, positive dark green

Explanation: Stacking two tab_style() calls with mutually exclusive rows = predicates is the canonical gt pattern for two-sided conditional formatting. The order matters when predicates overlap: later calls win, so put the more specific predicate last. For traffic-light tables (red/yellow/green) chain three tab_style() blocks with breakpoints like pnl < 0, pnl == 0, pnl > 0.

Section 5. Summary rows, footnotes, and source notes (4 problems)

Exercise 5.1: Add a totals row with grand_summary_rows

Task: The finance team wants a grand total of revenue at the bottom of the weekly snapshot. Take ex_3_1 and add grand_summary_rows(columns = c(signups, revenue_usd), fns = list(Total = ~sum(.)), fmt = ~ fmt_number(., decimals = 0, use_seps = TRUE)) to compute and display column totals. Save to ex_5_1.

Expected result:

<div id="qvqrbfvfft" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#qvqrbfvfft table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#qvqrbfvfft thead, #qvqrbfvfft tbody, #qvqrbfvfft tfoot, #qvqrbfvfft tr, #qvqrbfvfft td, #qvqrbfvfft th {
  border-style: none;
}

#qvqrbfvfft p {
  margin: 0;
  padding: 0;
}

#qvqrbfvfft .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#qvqrbfvfft .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#qvqrbfvfft .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#qvqrbfvfft .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#qvqrbfvfft .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#qvqrbfvfft .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qvqrbfvfft .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#qvqrbfvfft .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#qvqrbfvfft .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#qvqrbfvfft .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#qvqrbfvfft .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#qvqrbfvfft .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#qvqrbfvfft .gt_spanner_row {
  border-bottom-style: hidden;
}

#qvqrbfvfft .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#qvqrbfvfft .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#qvqrbfvfft .gt_from_md > :first-child {
  margin-top: 0;
}

#qvqrbfvfft .gt_from_md > :last-child {
  margin-bottom: 0;
}

#qvqrbfvfft .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#qvqrbfvfft .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#qvqrbfvfft .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#qvqrbfvfft .gt_row_group_first td {
  border-top-width: 2px;
}

#qvqrbfvfft .gt_row_group_first th {
  border-top-width: 2px;
}

#qvqrbfvfft .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#qvqrbfvfft .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#qvqrbfvfft .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#qvqrbfvfft .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qvqrbfvfft .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#qvqrbfvfft .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#qvqrbfvfft .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#qvqrbfvfft .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#qvqrbfvfft .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qvqrbfvfft .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#qvqrbfvfft .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#qvqrbfvfft .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#qvqrbfvfft .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#qvqrbfvfft .gt_left {
  text-align: left;
}

#qvqrbfvfft .gt_center {
  text-align: center;
}

#qvqrbfvfft .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#qvqrbfvfft .gt_font_normal {
  font-weight: normal;
}

#qvqrbfvfft .gt_font_bold {
  font-weight: bold;
}

#qvqrbfvfft .gt_font_italic {
  font-style: italic;
}

#qvqrbfvfft .gt_super {
  font-size: 65%;
}

#qvqrbfvfft .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#qvqrbfvfft .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#qvqrbfvfft .gt_indent_1 {
  text-indent: 5px;
}

#qvqrbfvfft .gt_indent_2 {
  text-indent: 10px;
}

#qvqrbfvfft .gt_indent_3 {
  text-indent: 15px;
}

#qvqrbfvfft .gt_indent_4 {
  text-indent: 20px;
}

#qvqrbfvfft .gt_indent_5 {
  text-indent: 25px;
}

#qvqrbfvfft .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#qvqrbfvfft div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_title gt_font_normal" style>Weekly Growth Snapshot</td>
    </tr>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_subtitle gt_font_normal gt_bottom_border" style>Signups and revenue, last 4 weeks</td>
    </tr>
    <tr class="gt_col_headings gt_spanner_row">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="2" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_center gt_columns_top_border gt_column_spanner_outer" rowspan="1" colspan="2" scope="colgroup" id="This week">
        <div class="gt_column_spanner">This week</div>
      </th>
    </tr>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="signups">New Signups</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="revenue_usd">Revenue (USD)</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">W1</th>
<td headers="stub_1_1 signups" class="gt_row gt_right">320</td>
<td headers="stub_1_1 revenue_usd" class="gt_row gt_right">4,820</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">W2</th>
<td headers="stub_1_2 signups" class="gt_row gt_right">410</td>
<td headers="stub_1_2 revenue_usd" class="gt_row gt_right">6,105</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">W3</th>
<td headers="stub_1_3 signups" class="gt_row gt_right">388</td>
<td headers="stub_1_3 revenue_usd" class="gt_row gt_right">5,740</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_left gt_stub">W4</th>
<td headers="stub_1_4 signups" class="gt_row gt_right">502</td>
<td headers="stub_1_4 revenue_usd" class="gt_row gt_right">7,888</td></tr>
    <tr><th id="grand_summary_stub_1" scope="row" class="gt_row gt_left gt_stub gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row">Total</th>
<td headers="grand_summary_stub_1 signups" class="gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row">1,620</td>
<td headers="grand_summary_stub_1 revenue_usd" class="gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row">24,553</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Advanced

RYour turn
ex_5_1 <- # your code here ex_5_1

Click to reveal solution
RSolution
ex_5_1 <- ex_3_1 |> grand_summary_rows( columns = c(signups, revenue_usd), fns = list(Total = ~sum(.)), fmt = ~ fmt_number(., decimals = 0, use_seps = TRUE) ) ex_5_1 #> A Total row appears below all data: signups=1,620, revenue_usd=24,553

Explanation: grand_summary_rows() aggregates the entire body (ignoring row groups), whereas summary_rows() aggregates within each row group. fns = is a named list of functions or lambdas; the names become row labels. fmt = reapplies number formatting because the aggregate value enters the table fresh and needs its own format pass, the column-level fmt_number() does not propagate.

Exercise 5.2: Add a footnote attached to a specific cell

Task: The compliance officer asks for a footnote on the W4 revenue cell explaining it includes a one-off enterprise deal. Take ex_5_1 and use tab_footnote(footnote = "Includes one enterprise deal worth $2,200.", locations = cells_body(columns = revenue_usd, rows = 4)) to attach a numbered footnote. Save to ex_5_2.

Expected result:

<div id="objwlziabx" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#objwlziabx table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#objwlziabx thead, #objwlziabx tbody, #objwlziabx tfoot, #objwlziabx tr, #objwlziabx td, #objwlziabx th {
  border-style: none;
}

#objwlziabx p {
  margin: 0;
  padding: 0;
}

#objwlziabx .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#objwlziabx .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#objwlziabx .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#objwlziabx .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#objwlziabx .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#objwlziabx .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#objwlziabx .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#objwlziabx .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#objwlziabx .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#objwlziabx .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#objwlziabx .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#objwlziabx .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#objwlziabx .gt_spanner_row {
  border-bottom-style: hidden;
}

#objwlziabx .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#objwlziabx .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#objwlziabx .gt_from_md > :first-child {
  margin-top: 0;
}

#objwlziabx .gt_from_md > :last-child {
  margin-bottom: 0;
}

#objwlziabx .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#objwlziabx .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#objwlziabx .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#objwlziabx .gt_row_group_first td {
  border-top-width: 2px;
}

#objwlziabx .gt_row_group_first th {
  border-top-width: 2px;
}

#objwlziabx .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#objwlziabx .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#objwlziabx .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#objwlziabx .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#objwlziabx .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#objwlziabx .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#objwlziabx .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#objwlziabx .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#objwlziabx .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#objwlziabx .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#objwlziabx .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#objwlziabx .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#objwlziabx .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#objwlziabx .gt_left {
  text-align: left;
}

#objwlziabx .gt_center {
  text-align: center;
}

#objwlziabx .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#objwlziabx .gt_font_normal {
  font-weight: normal;
}

#objwlziabx .gt_font_bold {
  font-weight: bold;
}

#objwlziabx .gt_font_italic {
  font-style: italic;
}

#objwlziabx .gt_super {
  font-size: 65%;
}

#objwlziabx .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#objwlziabx .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#objwlziabx .gt_indent_1 {
  text-indent: 5px;
}

#objwlziabx .gt_indent_2 {
  text-indent: 10px;
}

#objwlziabx .gt_indent_3 {
  text-indent: 15px;
}

#objwlziabx .gt_indent_4 {
  text-indent: 20px;
}

#objwlziabx .gt_indent_5 {
  text-indent: 25px;
}

#objwlziabx .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#objwlziabx div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_title gt_font_normal" style>Weekly Growth Snapshot</td>
    </tr>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_subtitle gt_font_normal gt_bottom_border" style>Signups and revenue, last 4 weeks</td>
    </tr>
    <tr class="gt_col_headings gt_spanner_row">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="2" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_center gt_columns_top_border gt_column_spanner_outer" rowspan="1" colspan="2" scope="colgroup" id="This week">
        <div class="gt_column_spanner">This week</div>
      </th>
    </tr>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="signups">New Signups</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="revenue_usd">Revenue (USD)</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">W1</th>
<td headers="stub_1_1 signups" class="gt_row gt_right">320</td>
<td headers="stub_1_1 revenue_usd" class="gt_row gt_right">4,820</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">W2</th>
<td headers="stub_1_2 signups" class="gt_row gt_right">410</td>
<td headers="stub_1_2 revenue_usd" class="gt_row gt_right">6,105</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">W3</th>
<td headers="stub_1_3 signups" class="gt_row gt_right">388</td>
<td headers="stub_1_3 revenue_usd" class="gt_row gt_right">5,740</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_left gt_stub">W4</th>
<td headers="stub_1_4 signups" class="gt_row gt_right">502</td>
<td headers="stub_1_4 revenue_usd" class="gt_row gt_right"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span> 7,888</td></tr>
    <tr><th id="grand_summary_stub_1" scope="row" class="gt_row gt_left gt_stub gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row">Total</th>
<td headers="grand_summary_stub_1 signups" class="gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row">1,620</td>
<td headers="grand_summary_stub_1 revenue_usd" class="gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row">24,553</td></tr>
  </tbody>
  <tfoot>
    <tr class="gt_footnotes">
      <td class="gt_footnote" colspan="3"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span> Includes one enterprise deal worth $2,200.</td>
    </tr>
  </tfoot>
</table>
</div>

Difficulty: Intermediate

RYour turn
ex_5_2 <- # your code here ex_5_2

Click to reveal solution
RSolution
ex_5_2 <- ex_5_1 |> tab_footnote( footnote = "Includes one enterprise deal worth $2,200.", locations = cells_body(columns = revenue_usd, rows = 4) ) ex_5_2 #> Footnote marker attached to W4 revenue cell

Explanation: tab_footnote() accepts the same locations = helpers as tab_style(), so you can attach notes to column labels, the title, summary rows, or any specific body cell. gt auto-numbers footnotes and rebuilds the markers if you add more later. For glyph-style markers (asterisks, daggers) pass opt_footnote_marks(marks = "standard") once at the end of the pipeline.

Exercise 5.3: Add a source note for data provenance

Task: The audit team requires every published table to cite its source. Take ex_5_2 and use tab_source_note(source_note = md("Source: Internal CRM, retrieved 2026-05-13.")) to add a small italicised source line under the table (the md() helper renders bold). Save to ex_5_3.

Expected result:

<div id="xbyknbbjyv" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#xbyknbbjyv table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#xbyknbbjyv thead, #xbyknbbjyv tbody, #xbyknbbjyv tfoot, #xbyknbbjyv tr, #xbyknbbjyv td, #xbyknbbjyv th {
  border-style: none;
}

#xbyknbbjyv p {
  margin: 0;
  padding: 0;
}

#xbyknbbjyv .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#xbyknbbjyv .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#xbyknbbjyv .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#xbyknbbjyv .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#xbyknbbjyv .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#xbyknbbjyv .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#xbyknbbjyv .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#xbyknbbjyv .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#xbyknbbjyv .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#xbyknbbjyv .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#xbyknbbjyv .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#xbyknbbjyv .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#xbyknbbjyv .gt_spanner_row {
  border-bottom-style: hidden;
}

#xbyknbbjyv .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#xbyknbbjyv .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#xbyknbbjyv .gt_from_md > :first-child {
  margin-top: 0;
}

#xbyknbbjyv .gt_from_md > :last-child {
  margin-bottom: 0;
}

#xbyknbbjyv .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#xbyknbbjyv .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#xbyknbbjyv .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#xbyknbbjyv .gt_row_group_first td {
  border-top-width: 2px;
}

#xbyknbbjyv .gt_row_group_first th {
  border-top-width: 2px;
}

#xbyknbbjyv .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#xbyknbbjyv .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#xbyknbbjyv .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#xbyknbbjyv .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#xbyknbbjyv .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#xbyknbbjyv .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#xbyknbbjyv .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#xbyknbbjyv .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#xbyknbbjyv .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#xbyknbbjyv .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#xbyknbbjyv .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#xbyknbbjyv .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#xbyknbbjyv .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#xbyknbbjyv .gt_left {
  text-align: left;
}

#xbyknbbjyv .gt_center {
  text-align: center;
}

#xbyknbbjyv .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#xbyknbbjyv .gt_font_normal {
  font-weight: normal;
}

#xbyknbbjyv .gt_font_bold {
  font-weight: bold;
}

#xbyknbbjyv .gt_font_italic {
  font-style: italic;
}

#xbyknbbjyv .gt_super {
  font-size: 65%;
}

#xbyknbbjyv .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#xbyknbbjyv .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#xbyknbbjyv .gt_indent_1 {
  text-indent: 5px;
}

#xbyknbbjyv .gt_indent_2 {
  text-indent: 10px;
}

#xbyknbbjyv .gt_indent_3 {
  text-indent: 15px;
}

#xbyknbbjyv .gt_indent_4 {
  text-indent: 20px;
}

#xbyknbbjyv .gt_indent_5 {
  text-indent: 25px;
}

#xbyknbbjyv .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#xbyknbbjyv div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_title gt_font_normal" style>Weekly Growth Snapshot</td>
    </tr>
    <tr class="gt_heading">
      <td colspan="3" class="gt_heading gt_subtitle gt_font_normal gt_bottom_border" style>Signups and revenue, last 4 weeks</td>
    </tr>
    <tr class="gt_col_headings gt_spanner_row">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="2" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_center gt_columns_top_border gt_column_spanner_outer" rowspan="1" colspan="2" scope="colgroup" id="This week">
        <div class="gt_column_spanner">This week</div>
      </th>
    </tr>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="signups">New Signups</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="revenue_usd">Revenue (USD)</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">W1</th>
<td headers="stub_1_1 signups" class="gt_row gt_right">320</td>
<td headers="stub_1_1 revenue_usd" class="gt_row gt_right">4,820</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">W2</th>
<td headers="stub_1_2 signups" class="gt_row gt_right">410</td>
<td headers="stub_1_2 revenue_usd" class="gt_row gt_right">6,105</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">W3</th>
<td headers="stub_1_3 signups" class="gt_row gt_right">388</td>
<td headers="stub_1_3 revenue_usd" class="gt_row gt_right">5,740</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_left gt_stub">W4</th>
<td headers="stub_1_4 signups" class="gt_row gt_right">502</td>
<td headers="stub_1_4 revenue_usd" class="gt_row gt_right"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span> 7,888</td></tr>
    <tr><th id="grand_summary_stub_1" scope="row" class="gt_row gt_left gt_stub gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row">Total</th>
<td headers="grand_summary_stub_1 signups" class="gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row">1,620</td>
<td headers="grand_summary_stub_1 revenue_usd" class="gt_row gt_right gt_grand_summary_row gt_first_grand_summary_row gt_last_summary_row">24,553</td></tr>
  </tbody>
  <tfoot>
    <tr class="gt_footnotes">
      <td class="gt_footnote" colspan="3"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span> Includes one enterprise deal worth $2,200.</td>
    </tr>
    <tr class="gt_sourcenotes">
      <td class="gt_sourcenote" colspan="3"><span class='gt_from_md'><strong>Source:</strong> Internal CRM, retrieved 2026-05-13.</span></td>
    </tr>
  </tfoot>
</table>
</div>

Difficulty: Intermediate

RYour turn
ex_5_3 <- # your code here ex_5_3

Click to reveal solution
RSolution
ex_5_3 <- ex_5_2 |> tab_source_note( source_note = md("**Source:** Internal CRM, retrieved 2026-05-13.") ) ex_5_3 #> A "Source:" line appears under the footnote

Explanation: Source notes sit at the very bottom of the table, below footnotes. They are conventionally used for citations, retrieval timestamps, or methodology pointers ("Excludes refunds"). The md() helper turns Markdown into HTML inside any gt string slot, so bold, italic, and [links](url) work. Use html() instead if you need raw tags or <sub>/<sup>.

Exercise 5.4: Group-level summary rows with summary_rows

Task: The audit team wants subtotals by row group. Build an inline tibble with region (East, East, West, West), product (A, B, A, B), and revenue (c(1200, 1500, 900, 1800)). Pipe into gt(rowname_col = "product", groupname_col = "region"), then use summary_rows(groups = TRUE, columns = revenue, fns = list(Subtotal = ~sum(.)), fmt = ~ fmt_number(., decimals = 0, use_seps = TRUE)) to compute per-region subtotals. Save to ex_5_4.

Expected result:

<div id="zaecvzmktz" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#zaecvzmktz table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#zaecvzmktz thead, #zaecvzmktz tbody, #zaecvzmktz tfoot, #zaecvzmktz tr, #zaecvzmktz td, #zaecvzmktz th {
  border-style: none;
}

#zaecvzmktz p {
  margin: 0;
  padding: 0;
}

#zaecvzmktz .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#zaecvzmktz .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#zaecvzmktz .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#zaecvzmktz .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#zaecvzmktz .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#zaecvzmktz .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#zaecvzmktz .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#zaecvzmktz .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#zaecvzmktz .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#zaecvzmktz .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#zaecvzmktz .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#zaecvzmktz .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#zaecvzmktz .gt_spanner_row {
  border-bottom-style: hidden;
}

#zaecvzmktz .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#zaecvzmktz .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#zaecvzmktz .gt_from_md > :first-child {
  margin-top: 0;
}

#zaecvzmktz .gt_from_md > :last-child {
  margin-bottom: 0;
}

#zaecvzmktz .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#zaecvzmktz .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#zaecvzmktz .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#zaecvzmktz .gt_row_group_first td {
  border-top-width: 2px;
}

#zaecvzmktz .gt_row_group_first th {
  border-top-width: 2px;
}

#zaecvzmktz .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#zaecvzmktz .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#zaecvzmktz .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#zaecvzmktz .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#zaecvzmktz .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#zaecvzmktz .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#zaecvzmktz .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#zaecvzmktz .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#zaecvzmktz .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#zaecvzmktz .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#zaecvzmktz .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#zaecvzmktz .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#zaecvzmktz .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#zaecvzmktz .gt_left {
  text-align: left;
}

#zaecvzmktz .gt_center {
  text-align: center;
}

#zaecvzmktz .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#zaecvzmktz .gt_font_normal {
  font-weight: normal;
}

#zaecvzmktz .gt_font_bold {
  font-weight: bold;
}

#zaecvzmktz .gt_font_italic {
  font-style: italic;
}

#zaecvzmktz .gt_super {
  font-size: 65%;
}

#zaecvzmktz .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#zaecvzmktz .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#zaecvzmktz .gt_indent_1 {
  text-indent: 5px;
}

#zaecvzmktz .gt_indent_2 {
  text-indent: 10px;
}

#zaecvzmktz .gt_indent_3 {
  text-indent: 15px;
}

#zaecvzmktz .gt_indent_4 {
  text-indent: 20px;
}

#zaecvzmktz .gt_indent_5 {
  text-indent: 25px;
}

#zaecvzmktz .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#zaecvzmktz div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="revenue">revenue</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr class="gt_group_heading_row">
      <th colspan="2" class="gt_group_heading" scope="colgroup" id="East">East</th>
    </tr>
    <tr class="gt_row_group_first"><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">A</th>
<td headers="East stub_1_1 revenue" class="gt_row gt_right">1200</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">B</th>
<td headers="East stub_1_2 revenue" class="gt_row gt_right">1500</td></tr>
    <tr class="gt_group_heading_row">
      <th colspan="2" class="gt_group_heading" scope="colgroup" id="West">West</th>
    </tr>
    <tr class="gt_row_group_first"><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">A</th>
<td headers="West stub_1_3 revenue" class="gt_row gt_right">900</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_left gt_stub">B</th>
<td headers="West stub_1_4 revenue" class="gt_row gt_right">1800</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Advanced

RYour turn
ex_5_4 <- # your code here ex_5_4

Click to reveal solution
RSolution
sales <- tibble( region = c("East", "East", "West", "West"), product = c("A", "B", "A", "B"), revenue = c(1200, 1500, 900, 1800) ) ex_5_4 <- gt(sales, rowname_col = "product", groupname_col = "region") |> summary_rows( groups = TRUE, columns = revenue, fns = list(Subtotal = ~sum(.)), fmt = ~ fmt_number(., decimals = 0, use_seps = TRUE) ) ex_5_4 #> Each region shows a Subtotal row right below its members

Explanation: summary_rows() with groups = TRUE runs the summary functions inside each row group, the table-level analogue of dplyr::group_by() |> summarise(). The groupname_col argument to gt() is what triggers the grouping; without it gt sees a flat table and summary_rows() has nothing to partition. Pair with grand_summary_rows() to get both per-group subtotals and a grand total.

Section 6. Row groups, alignment, and exporting (4 problems)

Exercise 6.1: Build a grouped table from mtcars by cyl

Task: The take-home interviewer wants a tidy view of mtcars grouped by cylinder count. Take mtcars, move row names into a model column, keep model, cyl, mpg, hp, then pipe into gt(rowname_col = "model", groupname_col = "cyl") to display rows grouped by cylinders. Save to ex_6_1.

Expected result:

<div id="qtdtkqiqmq" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#qtdtkqiqmq table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#qtdtkqiqmq thead, #qtdtkqiqmq tbody, #qtdtkqiqmq tfoot, #qtdtkqiqmq tr, #qtdtkqiqmq td, #qtdtkqiqmq th {
  border-style: none;
}

#qtdtkqiqmq p {
  margin: 0;
  padding: 0;
}

#qtdtkqiqmq .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#qtdtkqiqmq .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#qtdtkqiqmq .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#qtdtkqiqmq .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#qtdtkqiqmq .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#qtdtkqiqmq .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qtdtkqiqmq .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#qtdtkqiqmq .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#qtdtkqiqmq .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#qtdtkqiqmq .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#qtdtkqiqmq .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#qtdtkqiqmq .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#qtdtkqiqmq .gt_spanner_row {
  border-bottom-style: hidden;
}

#qtdtkqiqmq .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#qtdtkqiqmq .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#qtdtkqiqmq .gt_from_md > :first-child {
  margin-top: 0;
}

#qtdtkqiqmq .gt_from_md > :last-child {
  margin-bottom: 0;
}

#qtdtkqiqmq .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#qtdtkqiqmq .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#qtdtkqiqmq .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#qtdtkqiqmq .gt_row_group_first td {
  border-top-width: 2px;
}

#qtdtkqiqmq .gt_row_group_first th {
  border-top-width: 2px;
}

#qtdtkqiqmq .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#qtdtkqiqmq .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#qtdtkqiqmq .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#qtdtkqiqmq .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qtdtkqiqmq .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#qtdtkqiqmq .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#qtdtkqiqmq .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#qtdtkqiqmq .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#qtdtkqiqmq .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qtdtkqiqmq .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#qtdtkqiqmq .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#qtdtkqiqmq .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#qtdtkqiqmq .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#qtdtkqiqmq .gt_left {
  text-align: left;
}

#qtdtkqiqmq .gt_center {
  text-align: center;
}

#qtdtkqiqmq .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#qtdtkqiqmq .gt_font_normal {
  font-weight: normal;
}

#qtdtkqiqmq .gt_font_bold {
  font-weight: bold;
}

#qtdtkqiqmq .gt_font_italic {
  font-style: italic;
}

#qtdtkqiqmq .gt_super {
  font-size: 65%;
}

#qtdtkqiqmq .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#qtdtkqiqmq .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#qtdtkqiqmq .gt_indent_1 {
  text-indent: 5px;
}

#qtdtkqiqmq .gt_indent_2 {
  text-indent: 10px;
}

#qtdtkqiqmq .gt_indent_3 {
  text-indent: 15px;
}

#qtdtkqiqmq .gt_indent_4 {
  text-indent: 20px;
}

#qtdtkqiqmq .gt_indent_5 {
  text-indent: 25px;
}

#qtdtkqiqmq .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#qtdtkqiqmq div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="mpg">mpg</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="hp">hp</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr class="gt_group_heading_row">
      <th colspan="3" class="gt_group_heading" scope="colgroup" id="6">6</th>
    </tr>
    <tr class="gt_row_group_first"><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">Mazda RX4</th>
<td headers="6 stub_1_1 mpg" class="gt_row gt_right">21.0</td>
<td headers="6 stub_1_1 hp" class="gt_row gt_right">110</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">Mazda RX4 Wag</th>
<td headers="6 stub_1_2 mpg" class="gt_row gt_right">21.0</td>
<td headers="6 stub_1_2 hp" class="gt_row gt_right">110</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">Hornet 4 Drive</th>
<td headers="6 stub_1_3 mpg" class="gt_row gt_right">21.4</td>
<td headers="6 stub_1_3 hp" class="gt_row gt_right">110</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_left gt_stub">Valiant</th>
<td headers="6 stub_1_4 mpg" class="gt_row gt_right">18.1</td>
<td headers="6 stub_1_4 hp" class="gt_row gt_right">105</td></tr>
    <tr><th id="stub_1_5" scope="row" class="gt_row gt_left gt_stub">Merc 280</th>
<td headers="6 stub_1_5 mpg" class="gt_row gt_right">19.2</td>
<td headers="6 stub_1_5 hp" class="gt_row gt_right">123</td></tr>
    <tr><th id="stub_1_6" scope="row" class="gt_row gt_left gt_stub">Merc 280C</th>
<td headers="6 stub_1_6 mpg" class="gt_row gt_right">17.8</td>
<td headers="6 stub_1_6 hp" class="gt_row gt_right">123</td></tr>
    <tr><th id="stub_1_7" scope="row" class="gt_row gt_left gt_stub">Ferrari Dino</th>
<td headers="6 stub_1_7 mpg" class="gt_row gt_right">19.7</td>
<td headers="6 stub_1_7 hp" class="gt_row gt_right">175</td></tr>
    <tr class="gt_group_heading_row">
      <th colspan="3" class="gt_group_heading" scope="colgroup" id="4">4</th>
    </tr>
    <tr class="gt_row_group_first"><th id="stub_1_8" scope="row" class="gt_row gt_left gt_stub">Datsun 710</th>
<td headers="4 stub_1_8 mpg" class="gt_row gt_right">22.8</td>
<td headers="4 stub_1_8 hp" class="gt_row gt_right">93</td></tr>
    <tr><th id="stub_1_9" scope="row" class="gt_row gt_left gt_stub">Merc 240D</th>
<td headers="4 stub_1_9 mpg" class="gt_row gt_right">24.4</td>
<td headers="4 stub_1_9 hp" class="gt_row gt_right">62</td></tr>
    <tr><th id="stub_1_10" scope="row" class="gt_row gt_left gt_stub">Merc 230</th>
<td headers="4 stub_1_10 mpg" class="gt_row gt_right">22.8</td>
<td headers="4 stub_1_10 hp" class="gt_row gt_right">95</td></tr>
    <tr><th id="stub_1_11" scope="row" class="gt_row gt_left gt_stub">Fiat 128</th>
<td headers="4 stub_1_11 mpg" class="gt_row gt_right">32.4</td>
<td headers="4 stub_1_11 hp" class="gt_row gt_right">66</td></tr>
    <tr><th id="stub_1_12" scope="row" class="gt_row gt_left gt_stub">Honda Civic</th>
<td headers="4 stub_1_12 mpg" class="gt_row gt_right">30.4</td>
<td headers="4 stub_1_12 hp" class="gt_row gt_right">52</td></tr>
    <tr><th id="stub_1_13" scope="row" class="gt_row gt_left gt_stub">Toyota Corolla</th>
<td headers="4 stub_1_13 mpg" class="gt_row gt_right">33.9</td>
<td headers="4 stub_1_13 hp" class="gt_row gt_right">65</td></tr>
    <tr><th id="stub_1_14" scope="row" class="gt_row gt_left gt_stub">Toyota Corona</th>
<td headers="4 stub_1_14 mpg" class="gt_row gt_right">21.5</td>
<td headers="4 stub_1_14 hp" class="gt_row gt_right">97</td></tr>
    <tr><th id="stub_1_15" scope="row" class="gt_row gt_left gt_stub">Fiat X1-9</th>
<td headers="4 stub_1_15 mpg" class="gt_row gt_right">27.3</td>
<td headers="4 stub_1_15 hp" class="gt_row gt_right">66</td></tr>
    <tr><th id="stub_1_16" scope="row" class="gt_row gt_left gt_stub">Porsche 914-2</th>
<td headers="4 stub_1_16 mpg" class="gt_row gt_right">26.0</td>
<td headers="4 stub_1_16 hp" class="gt_row gt_right">91</td></tr>
    <tr><th id="stub_1_17" scope="row" class="gt_row gt_left gt_stub">Lotus Europa</th>
<td headers="4 stub_1_17 mpg" class="gt_row gt_right">30.4</td>
<td headers="4 stub_1_17 hp" class="gt_row gt_right">113</td></tr>
    <tr><th id="stub_1_18" scope="row" class="gt_row gt_left gt_stub">Volvo 142E</th>
<td headers="4 stub_1_18 mpg" class="gt_row gt_right">21.4</td>
<td headers="4 stub_1_18 hp" class="gt_row gt_right">109</td></tr>
    <tr class="gt_group_heading_row">
      <th colspan="3" class="gt_group_heading" scope="colgroup" id="8">8</th>
    </tr>
    <tr class="gt_row_group_first"><th id="stub_1_19" scope="row" class="gt_row gt_left gt_stub">Hornet Sportabout</th>
<td headers="8 stub_1_19 mpg" class="gt_row gt_right">18.7</td>
<td headers="8 stub_1_19 hp" class="gt_row gt_right">175</td></tr>
    <tr><th id="stub_1_20" scope="row" class="gt_row gt_left gt_stub">Duster 360</th>
<td headers="8 stub_1_20 mpg" class="gt_row gt_right">14.3</td>
<td headers="8 stub_1_20 hp" class="gt_row gt_right">245</td></tr>
    <tr><th id="stub_1_21" scope="row" class="gt_row gt_left gt_stub">Merc 450SE</th>
<td headers="8 stub_1_21 mpg" class="gt_row gt_right">16.4</td>
<td headers="8 stub_1_21 hp" class="gt_row gt_right">180</td></tr>
    <tr><th id="stub_1_22" scope="row" class="gt_row gt_left gt_stub">Merc 450SL</th>
<td headers="8 stub_1_22 mpg" class="gt_row gt_right">17.3</td>
<td headers="8 stub_1_22 hp" class="gt_row gt_right">180</td></tr>
    <tr><th id="stub_1_23" scope="row" class="gt_row gt_left gt_stub">Merc 450SLC</th>
<td headers="8 stub_1_23 mpg" class="gt_row gt_right">15.2</td>
<td headers="8 stub_1_23 hp" class="gt_row gt_right">180</td></tr>
    <tr><th id="stub_1_24" scope="row" class="gt_row gt_left gt_stub">Cadillac Fleetwood</th>
<td headers="8 stub_1_24 mpg" class="gt_row gt_right">10.4</td>
<td headers="8 stub_1_24 hp" class="gt_row gt_right">205</td></tr>
    <tr><th id="stub_1_25" scope="row" class="gt_row gt_left gt_stub">Lincoln Continental</th>
<td headers="8 stub_1_25 mpg" class="gt_row gt_right">10.4</td>
<td headers="8 stub_1_25 hp" class="gt_row gt_right">215</td></tr>
    <tr><th id="stub_1_26" scope="row" class="gt_row gt_left gt_stub">Chrysler Imperial</th>
<td headers="8 stub_1_26 mpg" class="gt_row gt_right">14.7</td>
<td headers="8 stub_1_26 hp" class="gt_row gt_right">230</td></tr>
    <tr><th id="stub_1_27" scope="row" class="gt_row gt_left gt_stub">Dodge Challenger</th>
<td headers="8 stub_1_27 mpg" class="gt_row gt_right">15.5</td>
<td headers="8 stub_1_27 hp" class="gt_row gt_right">150</td></tr>
    <tr><th id="stub_1_28" scope="row" class="gt_row gt_left gt_stub">AMC Javelin</th>
<td headers="8 stub_1_28 mpg" class="gt_row gt_right">15.2</td>
<td headers="8 stub_1_28 hp" class="gt_row gt_right">150</td></tr>
    <tr><th id="stub_1_29" scope="row" class="gt_row gt_left gt_stub">Camaro Z28</th>
<td headers="8 stub_1_29 mpg" class="gt_row gt_right">13.3</td>
<td headers="8 stub_1_29 hp" class="gt_row gt_right">245</td></tr>
    <tr><th id="stub_1_30" scope="row" class="gt_row gt_left gt_stub">Pontiac Firebird</th>
<td headers="8 stub_1_30 mpg" class="gt_row gt_right">19.2</td>
<td headers="8 stub_1_30 hp" class="gt_row gt_right">175</td></tr>
    <tr><th id="stub_1_31" scope="row" class="gt_row gt_left gt_stub">Ford Pantera L</th>
<td headers="8 stub_1_31 mpg" class="gt_row gt_right">15.8</td>
<td headers="8 stub_1_31 hp" class="gt_row gt_right">264</td></tr>
    <tr><th id="stub_1_32" scope="row" class="gt_row gt_left gt_stub">Maserati Bora</th>
<td headers="8 stub_1_32 mpg" class="gt_row gt_right">15.0</td>
<td headers="8 stub_1_32 hp" class="gt_row gt_right">335</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Intermediate

RYour turn
ex_6_1 <- # your code here ex_6_1

Click to reveal solution
RSolution
ex_6_1 <- mtcars |> tibble::rownames_to_column("model") |> select(model, cyl, mpg, hp) |> gt(rowname_col = "model", groupname_col = "cyl") ex_6_1 #> 3 group banners: 4, 6, 8, each containing the matching models

Explanation: groupname_col tells gt which existing column to lift into row group banners. Rows are presented in the order they appear in the data, so to get a clean group order you typically arrange(cyl) first. The grouped layout is the foundation for summary_rows(groups = TRUE), group-level styling with cells_row_groups(), and row_group_order() to override the natural order.

Exercise 6.2: Re-order row groups with row_group_order

Task: The interviewer wants the cylinders displayed in descending order (8, 6, 4) for emphasis on muscle cars. Take ex_6_1 and apply row_group_order(groups = c("8", "6", "4")) to reorder the groups. Save to ex_6_2.

Expected result:

<div id="taqabhpcem" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#taqabhpcem table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#taqabhpcem thead, #taqabhpcem tbody, #taqabhpcem tfoot, #taqabhpcem tr, #taqabhpcem td, #taqabhpcem th {
  border-style: none;
}

#taqabhpcem p {
  margin: 0;
  padding: 0;
}

#taqabhpcem .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#taqabhpcem .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#taqabhpcem .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#taqabhpcem .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#taqabhpcem .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#taqabhpcem .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#taqabhpcem .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#taqabhpcem .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#taqabhpcem .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#taqabhpcem .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#taqabhpcem .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#taqabhpcem .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#taqabhpcem .gt_spanner_row {
  border-bottom-style: hidden;
}

#taqabhpcem .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#taqabhpcem .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#taqabhpcem .gt_from_md > :first-child {
  margin-top: 0;
}

#taqabhpcem .gt_from_md > :last-child {
  margin-bottom: 0;
}

#taqabhpcem .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#taqabhpcem .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#taqabhpcem .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#taqabhpcem .gt_row_group_first td {
  border-top-width: 2px;
}

#taqabhpcem .gt_row_group_first th {
  border-top-width: 2px;
}

#taqabhpcem .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#taqabhpcem .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#taqabhpcem .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#taqabhpcem .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#taqabhpcem .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#taqabhpcem .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#taqabhpcem .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#taqabhpcem .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#taqabhpcem .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#taqabhpcem .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#taqabhpcem .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#taqabhpcem .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#taqabhpcem .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#taqabhpcem .gt_left {
  text-align: left;
}

#taqabhpcem .gt_center {
  text-align: center;
}

#taqabhpcem .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#taqabhpcem .gt_font_normal {
  font-weight: normal;
}

#taqabhpcem .gt_font_bold {
  font-weight: bold;
}

#taqabhpcem .gt_font_italic {
  font-style: italic;
}

#taqabhpcem .gt_super {
  font-size: 65%;
}

#taqabhpcem .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#taqabhpcem .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#taqabhpcem .gt_indent_1 {
  text-indent: 5px;
}

#taqabhpcem .gt_indent_2 {
  text-indent: 10px;
}

#taqabhpcem .gt_indent_3 {
  text-indent: 15px;
}

#taqabhpcem .gt_indent_4 {
  text-indent: 20px;
}

#taqabhpcem .gt_indent_5 {
  text-indent: 25px;
}

#taqabhpcem .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#taqabhpcem div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="mpg">mpg</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="hp">hp</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr class="gt_group_heading_row">
      <th colspan="3" class="gt_group_heading" scope="colgroup" id="8">8</th>
    </tr>
    <tr class="gt_row_group_first"><th id="stub_1_1" scope="row" class="gt_row gt_left gt_stub">Hornet Sportabout</th>
<td headers="8 stub_1_1 mpg" class="gt_row gt_right">18.7</td>
<td headers="8 stub_1_1 hp" class="gt_row gt_right">175</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_left gt_stub">Duster 360</th>
<td headers="8 stub_1_2 mpg" class="gt_row gt_right">14.3</td>
<td headers="8 stub_1_2 hp" class="gt_row gt_right">245</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_left gt_stub">Merc 450SE</th>
<td headers="8 stub_1_3 mpg" class="gt_row gt_right">16.4</td>
<td headers="8 stub_1_3 hp" class="gt_row gt_right">180</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_left gt_stub">Merc 450SL</th>
<td headers="8 stub_1_4 mpg" class="gt_row gt_right">17.3</td>
<td headers="8 stub_1_4 hp" class="gt_row gt_right">180</td></tr>
    <tr><th id="stub_1_5" scope="row" class="gt_row gt_left gt_stub">Merc 450SLC</th>
<td headers="8 stub_1_5 mpg" class="gt_row gt_right">15.2</td>
<td headers="8 stub_1_5 hp" class="gt_row gt_right">180</td></tr>
    <tr><th id="stub_1_6" scope="row" class="gt_row gt_left gt_stub">Cadillac Fleetwood</th>
<td headers="8 stub_1_6 mpg" class="gt_row gt_right">10.4</td>
<td headers="8 stub_1_6 hp" class="gt_row gt_right">205</td></tr>
    <tr><th id="stub_1_7" scope="row" class="gt_row gt_left gt_stub">Lincoln Continental</th>
<td headers="8 stub_1_7 mpg" class="gt_row gt_right">10.4</td>
<td headers="8 stub_1_7 hp" class="gt_row gt_right">215</td></tr>
    <tr><th id="stub_1_8" scope="row" class="gt_row gt_left gt_stub">Chrysler Imperial</th>
<td headers="8 stub_1_8 mpg" class="gt_row gt_right">14.7</td>
<td headers="8 stub_1_8 hp" class="gt_row gt_right">230</td></tr>
    <tr><th id="stub_1_9" scope="row" class="gt_row gt_left gt_stub">Dodge Challenger</th>
<td headers="8 stub_1_9 mpg" class="gt_row gt_right">15.5</td>
<td headers="8 stub_1_9 hp" class="gt_row gt_right">150</td></tr>
    <tr><th id="stub_1_10" scope="row" class="gt_row gt_left gt_stub">AMC Javelin</th>
<td headers="8 stub_1_10 mpg" class="gt_row gt_right">15.2</td>
<td headers="8 stub_1_10 hp" class="gt_row gt_right">150</td></tr>
    <tr><th id="stub_1_11" scope="row" class="gt_row gt_left gt_stub">Camaro Z28</th>
<td headers="8 stub_1_11 mpg" class="gt_row gt_right">13.3</td>
<td headers="8 stub_1_11 hp" class="gt_row gt_right">245</td></tr>
    <tr><th id="stub_1_12" scope="row" class="gt_row gt_left gt_stub">Pontiac Firebird</th>
<td headers="8 stub_1_12 mpg" class="gt_row gt_right">19.2</td>
<td headers="8 stub_1_12 hp" class="gt_row gt_right">175</td></tr>
    <tr><th id="stub_1_13" scope="row" class="gt_row gt_left gt_stub">Ford Pantera L</th>
<td headers="8 stub_1_13 mpg" class="gt_row gt_right">15.8</td>
<td headers="8 stub_1_13 hp" class="gt_row gt_right">264</td></tr>
    <tr><th id="stub_1_14" scope="row" class="gt_row gt_left gt_stub">Maserati Bora</th>
<td headers="8 stub_1_14 mpg" class="gt_row gt_right">15.0</td>
<td headers="8 stub_1_14 hp" class="gt_row gt_right">335</td></tr>
    <tr class="gt_group_heading_row">
      <th colspan="3" class="gt_group_heading" scope="colgroup" id="6">6</th>
    </tr>
    <tr class="gt_row_group_first"><th id="stub_1_15" scope="row" class="gt_row gt_left gt_stub">Mazda RX4</th>
<td headers="6 stub_1_15 mpg" class="gt_row gt_right">21.0</td>
<td headers="6 stub_1_15 hp" class="gt_row gt_right">110</td></tr>
    <tr><th id="stub_1_16" scope="row" class="gt_row gt_left gt_stub">Mazda RX4 Wag</th>
<td headers="6 stub_1_16 mpg" class="gt_row gt_right">21.0</td>
<td headers="6 stub_1_16 hp" class="gt_row gt_right">110</td></tr>
    <tr><th id="stub_1_17" scope="row" class="gt_row gt_left gt_stub">Hornet 4 Drive</th>
<td headers="6 stub_1_17 mpg" class="gt_row gt_right">21.4</td>
<td headers="6 stub_1_17 hp" class="gt_row gt_right">110</td></tr>
    <tr><th id="stub_1_18" scope="row" class="gt_row gt_left gt_stub">Valiant</th>
<td headers="6 stub_1_18 mpg" class="gt_row gt_right">18.1</td>
<td headers="6 stub_1_18 hp" class="gt_row gt_right">105</td></tr>
    <tr><th id="stub_1_19" scope="row" class="gt_row gt_left gt_stub">Merc 280</th>
<td headers="6 stub_1_19 mpg" class="gt_row gt_right">19.2</td>
<td headers="6 stub_1_19 hp" class="gt_row gt_right">123</td></tr>
    <tr><th id="stub_1_20" scope="row" class="gt_row gt_left gt_stub">Merc 280C</th>
<td headers="6 stub_1_20 mpg" class="gt_row gt_right">17.8</td>
<td headers="6 stub_1_20 hp" class="gt_row gt_right">123</td></tr>
    <tr><th id="stub_1_21" scope="row" class="gt_row gt_left gt_stub">Ferrari Dino</th>
<td headers="6 stub_1_21 mpg" class="gt_row gt_right">19.7</td>
<td headers="6 stub_1_21 hp" class="gt_row gt_right">175</td></tr>
    <tr class="gt_group_heading_row">
      <th colspan="3" class="gt_group_heading" scope="colgroup" id="4">4</th>
    </tr>
    <tr class="gt_row_group_first"><th id="stub_1_22" scope="row" class="gt_row gt_left gt_stub">Datsun 710</th>
<td headers="4 stub_1_22 mpg" class="gt_row gt_right">22.8</td>
<td headers="4 stub_1_22 hp" class="gt_row gt_right">93</td></tr>
    <tr><th id="stub_1_23" scope="row" class="gt_row gt_left gt_stub">Merc 240D</th>
<td headers="4 stub_1_23 mpg" class="gt_row gt_right">24.4</td>
<td headers="4 stub_1_23 hp" class="gt_row gt_right">62</td></tr>
    <tr><th id="stub_1_24" scope="row" class="gt_row gt_left gt_stub">Merc 230</th>
<td headers="4 stub_1_24 mpg" class="gt_row gt_right">22.8</td>
<td headers="4 stub_1_24 hp" class="gt_row gt_right">95</td></tr>
    <tr><th id="stub_1_25" scope="row" class="gt_row gt_left gt_stub">Fiat 128</th>
<td headers="4 stub_1_25 mpg" class="gt_row gt_right">32.4</td>
<td headers="4 stub_1_25 hp" class="gt_row gt_right">66</td></tr>
    <tr><th id="stub_1_26" scope="row" class="gt_row gt_left gt_stub">Honda Civic</th>
<td headers="4 stub_1_26 mpg" class="gt_row gt_right">30.4</td>
<td headers="4 stub_1_26 hp" class="gt_row gt_right">52</td></tr>
    <tr><th id="stub_1_27" scope="row" class="gt_row gt_left gt_stub">Toyota Corolla</th>
<td headers="4 stub_1_27 mpg" class="gt_row gt_right">33.9</td>
<td headers="4 stub_1_27 hp" class="gt_row gt_right">65</td></tr>
    <tr><th id="stub_1_28" scope="row" class="gt_row gt_left gt_stub">Toyota Corona</th>
<td headers="4 stub_1_28 mpg" class="gt_row gt_right">21.5</td>
<td headers="4 stub_1_28 hp" class="gt_row gt_right">97</td></tr>
    <tr><th id="stub_1_29" scope="row" class="gt_row gt_left gt_stub">Fiat X1-9</th>
<td headers="4 stub_1_29 mpg" class="gt_row gt_right">27.3</td>
<td headers="4 stub_1_29 hp" class="gt_row gt_right">66</td></tr>
    <tr><th id="stub_1_30" scope="row" class="gt_row gt_left gt_stub">Porsche 914-2</th>
<td headers="4 stub_1_30 mpg" class="gt_row gt_right">26.0</td>
<td headers="4 stub_1_30 hp" class="gt_row gt_right">91</td></tr>
    <tr><th id="stub_1_31" scope="row" class="gt_row gt_left gt_stub">Lotus Europa</th>
<td headers="4 stub_1_31 mpg" class="gt_row gt_right">30.4</td>
<td headers="4 stub_1_31 hp" class="gt_row gt_right">113</td></tr>
    <tr><th id="stub_1_32" scope="row" class="gt_row gt_left gt_stub">Volvo 142E</th>
<td headers="4 stub_1_32 mpg" class="gt_row gt_right">21.4</td>
<td headers="4 stub_1_32 hp" class="gt_row gt_right">109</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Intermediate

RYour turn
ex_6_2 <- # your code here ex_6_2

Click to reveal solution
RSolution
ex_6_2 <- ex_6_1 |> row_group_order(groups = c("8", "6", "4")) ex_6_2 #> Groups now appear as: 8 cyl, 6 cyl, 4 cyl

Explanation: Group labels are stored as character strings, so even if the original column is numeric you pass the values as strings to row_group_order(). The function reorders only the named groups and leaves any unmentioned groups in their natural position. For full alphabetical or numeric reordering, arrange(desc(cyl)) upstream is simpler than enumerating groups.

Exercise 6.3: Right-align numeric columns and centre the stub

Task: A code reviewer asks for explicit alignment to avoid relying on gt defaults. Take ex_6_2 and use cols_align(align = "right", columns = c(mpg, hp)) and cols_align(align = "center", columns = model) to right-align the numerics and centre the stub column. Save to ex_6_3.

Expected result:

<div id="laseoxfwpj" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
  <style>#laseoxfwpj table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#laseoxfwpj thead, #laseoxfwpj tbody, #laseoxfwpj tfoot, #laseoxfwpj tr, #laseoxfwpj td, #laseoxfwpj th {
  border-style: none;
}

#laseoxfwpj p {
  margin: 0;
  padding: 0;
}

#laseoxfwpj .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#laseoxfwpj .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#laseoxfwpj .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#laseoxfwpj .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#laseoxfwpj .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#laseoxfwpj .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#laseoxfwpj .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#laseoxfwpj .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#laseoxfwpj .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#laseoxfwpj .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#laseoxfwpj .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#laseoxfwpj .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#laseoxfwpj .gt_spanner_row {
  border-bottom-style: hidden;
}

#laseoxfwpj .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#laseoxfwpj .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#laseoxfwpj .gt_from_md > :first-child {
  margin-top: 0;
}

#laseoxfwpj .gt_from_md > :last-child {
  margin-bottom: 0;
}

#laseoxfwpj .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#laseoxfwpj .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#laseoxfwpj .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#laseoxfwpj .gt_row_group_first td {
  border-top-width: 2px;
}

#laseoxfwpj .gt_row_group_first th {
  border-top-width: 2px;
}

#laseoxfwpj .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#laseoxfwpj .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#laseoxfwpj .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#laseoxfwpj .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#laseoxfwpj .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#laseoxfwpj .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#laseoxfwpj .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#laseoxfwpj .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#laseoxfwpj .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#laseoxfwpj .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#laseoxfwpj .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#laseoxfwpj .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#laseoxfwpj .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#laseoxfwpj .gt_left {
  text-align: left;
}

#laseoxfwpj .gt_center {
  text-align: center;
}

#laseoxfwpj .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#laseoxfwpj .gt_font_normal {
  font-weight: normal;
}

#laseoxfwpj .gt_font_bold {
  font-weight: bold;
}

#laseoxfwpj .gt_font_italic {
  font-style: italic;
}

#laseoxfwpj .gt_super {
  font-size: 65%;
}

#laseoxfwpj .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#laseoxfwpj .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#laseoxfwpj .gt_indent_1 {
  text-indent: 5px;
}

#laseoxfwpj .gt_indent_2 {
  text-indent: 10px;
}

#laseoxfwpj .gt_indent_3 {
  text-indent: 15px;
}

#laseoxfwpj .gt_indent_4 {
  text-indent: 20px;
}

#laseoxfwpj .gt_indent_5 {
  text-indent: 25px;
}

#laseoxfwpj .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#laseoxfwpj div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>
  <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
  <thead>
    <tr class="gt_col_headings">
      <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="a::stub"></th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="mpg">mpg</th>
      <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="hp">hp</th>
    </tr>
  </thead>
  <tbody class="gt_table_body">
    <tr class="gt_group_heading_row">
      <th colspan="3" class="gt_group_heading" scope="colgroup" id="8">8</th>
    </tr>
    <tr class="gt_row_group_first"><th id="stub_1_1" scope="row" class="gt_row gt_center gt_stub">Hornet Sportabout</th>
<td headers="8 stub_1_1 mpg" class="gt_row gt_right">18.7</td>
<td headers="8 stub_1_1 hp" class="gt_row gt_right">175</td></tr>
    <tr><th id="stub_1_2" scope="row" class="gt_row gt_center gt_stub">Duster 360</th>
<td headers="8 stub_1_2 mpg" class="gt_row gt_right">14.3</td>
<td headers="8 stub_1_2 hp" class="gt_row gt_right">245</td></tr>
    <tr><th id="stub_1_3" scope="row" class="gt_row gt_center gt_stub">Merc 450SE</th>
<td headers="8 stub_1_3 mpg" class="gt_row gt_right">16.4</td>
<td headers="8 stub_1_3 hp" class="gt_row gt_right">180</td></tr>
    <tr><th id="stub_1_4" scope="row" class="gt_row gt_center gt_stub">Merc 450SL</th>
<td headers="8 stub_1_4 mpg" class="gt_row gt_right">17.3</td>
<td headers="8 stub_1_4 hp" class="gt_row gt_right">180</td></tr>
    <tr><th id="stub_1_5" scope="row" class="gt_row gt_center gt_stub">Merc 450SLC</th>
<td headers="8 stub_1_5 mpg" class="gt_row gt_right">15.2</td>
<td headers="8 stub_1_5 hp" class="gt_row gt_right">180</td></tr>
    <tr><th id="stub_1_6" scope="row" class="gt_row gt_center gt_stub">Cadillac Fleetwood</th>
<td headers="8 stub_1_6 mpg" class="gt_row gt_right">10.4</td>
<td headers="8 stub_1_6 hp" class="gt_row gt_right">205</td></tr>
    <tr><th id="stub_1_7" scope="row" class="gt_row gt_center gt_stub">Lincoln Continental</th>
<td headers="8 stub_1_7 mpg" class="gt_row gt_right">10.4</td>
<td headers="8 stub_1_7 hp" class="gt_row gt_right">215</td></tr>
    <tr><th id="stub_1_8" scope="row" class="gt_row gt_center gt_stub">Chrysler Imperial</th>
<td headers="8 stub_1_8 mpg" class="gt_row gt_right">14.7</td>
<td headers="8 stub_1_8 hp" class="gt_row gt_right">230</td></tr>
    <tr><th id="stub_1_9" scope="row" class="gt_row gt_center gt_stub">Dodge Challenger</th>
<td headers="8 stub_1_9 mpg" class="gt_row gt_right">15.5</td>
<td headers="8 stub_1_9 hp" class="gt_row gt_right">150</td></tr>
    <tr><th id="stub_1_10" scope="row" class="gt_row gt_center gt_stub">AMC Javelin</th>
<td headers="8 stub_1_10 mpg" class="gt_row gt_right">15.2</td>
<td headers="8 stub_1_10 hp" class="gt_row gt_right">150</td></tr>
    <tr><th id="stub_1_11" scope="row" class="gt_row gt_center gt_stub">Camaro Z28</th>
<td headers="8 stub_1_11 mpg" class="gt_row gt_right">13.3</td>
<td headers="8 stub_1_11 hp" class="gt_row gt_right">245</td></tr>
    <tr><th id="stub_1_12" scope="row" class="gt_row gt_center gt_stub">Pontiac Firebird</th>
<td headers="8 stub_1_12 mpg" class="gt_row gt_right">19.2</td>
<td headers="8 stub_1_12 hp" class="gt_row gt_right">175</td></tr>
    <tr><th id="stub_1_13" scope="row" class="gt_row gt_center gt_stub">Ford Pantera L</th>
<td headers="8 stub_1_13 mpg" class="gt_row gt_right">15.8</td>
<td headers="8 stub_1_13 hp" class="gt_row gt_right">264</td></tr>
    <tr><th id="stub_1_14" scope="row" class="gt_row gt_center gt_stub">Maserati Bora</th>
<td headers="8 stub_1_14 mpg" class="gt_row gt_right">15.0</td>
<td headers="8 stub_1_14 hp" class="gt_row gt_right">335</td></tr>
    <tr class="gt_group_heading_row">
      <th colspan="3" class="gt_group_heading" scope="colgroup" id="6">6</th>
    </tr>
    <tr class="gt_row_group_first"><th id="stub_1_15" scope="row" class="gt_row gt_center gt_stub">Mazda RX4</th>
<td headers="6 stub_1_15 mpg" class="gt_row gt_right">21.0</td>
<td headers="6 stub_1_15 hp" class="gt_row gt_right">110</td></tr>
    <tr><th id="stub_1_16" scope="row" class="gt_row gt_center gt_stub">Mazda RX4 Wag</th>
<td headers="6 stub_1_16 mpg" class="gt_row gt_right">21.0</td>
<td headers="6 stub_1_16 hp" class="gt_row gt_right">110</td></tr>
    <tr><th id="stub_1_17" scope="row" class="gt_row gt_center gt_stub">Hornet 4 Drive</th>
<td headers="6 stub_1_17 mpg" class="gt_row gt_right">21.4</td>
<td headers="6 stub_1_17 hp" class="gt_row gt_right">110</td></tr>
    <tr><th id="stub_1_18" scope="row" class="gt_row gt_center gt_stub">Valiant</th>
<td headers="6 stub_1_18 mpg" class="gt_row gt_right">18.1</td>
<td headers="6 stub_1_18 hp" class="gt_row gt_right">105</td></tr>
    <tr><th id="stub_1_19" scope="row" class="gt_row gt_center gt_stub">Merc 280</th>
<td headers="6 stub_1_19 mpg" class="gt_row gt_right">19.2</td>
<td headers="6 stub_1_19 hp" class="gt_row gt_right">123</td></tr>
    <tr><th id="stub_1_20" scope="row" class="gt_row gt_center gt_stub">Merc 280C</th>
<td headers="6 stub_1_20 mpg" class="gt_row gt_right">17.8</td>
<td headers="6 stub_1_20 hp" class="gt_row gt_right">123</td></tr>
    <tr><th id="stub_1_21" scope="row" class="gt_row gt_center gt_stub">Ferrari Dino</th>
<td headers="6 stub_1_21 mpg" class="gt_row gt_right">19.7</td>
<td headers="6 stub_1_21 hp" class="gt_row gt_right">175</td></tr>
    <tr class="gt_group_heading_row">
      <th colspan="3" class="gt_group_heading" scope="colgroup" id="4">4</th>
    </tr>
    <tr class="gt_row_group_first"><th id="stub_1_22" scope="row" class="gt_row gt_center gt_stub">Datsun 710</th>
<td headers="4 stub_1_22 mpg" class="gt_row gt_right">22.8</td>
<td headers="4 stub_1_22 hp" class="gt_row gt_right">93</td></tr>
    <tr><th id="stub_1_23" scope="row" class="gt_row gt_center gt_stub">Merc 240D</th>
<td headers="4 stub_1_23 mpg" class="gt_row gt_right">24.4</td>
<td headers="4 stub_1_23 hp" class="gt_row gt_right">62</td></tr>
    <tr><th id="stub_1_24" scope="row" class="gt_row gt_center gt_stub">Merc 230</th>
<td headers="4 stub_1_24 mpg" class="gt_row gt_right">22.8</td>
<td headers="4 stub_1_24 hp" class="gt_row gt_right">95</td></tr>
    <tr><th id="stub_1_25" scope="row" class="gt_row gt_center gt_stub">Fiat 128</th>
<td headers="4 stub_1_25 mpg" class="gt_row gt_right">32.4</td>
<td headers="4 stub_1_25 hp" class="gt_row gt_right">66</td></tr>
    <tr><th id="stub_1_26" scope="row" class="gt_row gt_center gt_stub">Honda Civic</th>
<td headers="4 stub_1_26 mpg" class="gt_row gt_right">30.4</td>
<td headers="4 stub_1_26 hp" class="gt_row gt_right">52</td></tr>
    <tr><th id="stub_1_27" scope="row" class="gt_row gt_center gt_stub">Toyota Corolla</th>
<td headers="4 stub_1_27 mpg" class="gt_row gt_right">33.9</td>
<td headers="4 stub_1_27 hp" class="gt_row gt_right">65</td></tr>
    <tr><th id="stub_1_28" scope="row" class="gt_row gt_center gt_stub">Toyota Corona</th>
<td headers="4 stub_1_28 mpg" class="gt_row gt_right">21.5</td>
<td headers="4 stub_1_28 hp" class="gt_row gt_right">97</td></tr>
    <tr><th id="stub_1_29" scope="row" class="gt_row gt_center gt_stub">Fiat X1-9</th>
<td headers="4 stub_1_29 mpg" class="gt_row gt_right">27.3</td>
<td headers="4 stub_1_29 hp" class="gt_row gt_right">66</td></tr>
    <tr><th id="stub_1_30" scope="row" class="gt_row gt_center gt_stub">Porsche 914-2</th>
<td headers="4 stub_1_30 mpg" class="gt_row gt_right">26.0</td>
<td headers="4 stub_1_30 hp" class="gt_row gt_right">91</td></tr>
    <tr><th id="stub_1_31" scope="row" class="gt_row gt_center gt_stub">Lotus Europa</th>
<td headers="4 stub_1_31 mpg" class="gt_row gt_right">30.4</td>
<td headers="4 stub_1_31 hp" class="gt_row gt_right">113</td></tr>
    <tr><th id="stub_1_32" scope="row" class="gt_row gt_center gt_stub">Volvo 142E</th>
<td headers="4 stub_1_32 mpg" class="gt_row gt_right">21.4</td>
<td headers="4 stub_1_32 hp" class="gt_row gt_right">109</td></tr>
  </tbody>
  
</table>
</div>

Difficulty: Intermediate

RYour turn
ex_6_3 <- # your code here ex_6_3

Click to reveal solution
RSolution
ex_6_3 <- ex_6_2 |> cols_align(align = "right", columns = c(mpg, hp)) |> cols_align(align = "center", columns = model) ex_6_3 #> Numerics right-aligned, model centred

Explanation: cols_align() accepts "left", "center", "right", or "auto" and applies it to both the column label and every body cell. gt picks "right" for numerics and "left" for character columns by default, but forcing alignment explicitly makes the pipeline robust against data-type drift (a column that was numeric in dev but lands as character in prod). For the stub specifically, cols_align(columns = model) is the right move since the stub is just another column from gt's API perspective.

Exercise 6.4: Save the final table to disk with gtsave

Task: The reporting analyst needs the final artifact exported as standalone HTML for the boardroom. Take ex_6_3 and use gtsave(filename = tempfile(fileext = ".html")) to write a self-contained HTML file (a temp path keeps the exercise sandboxable). Capture the returned path into ex_6_4 so you can check the file exists.

Expected result:

[1] TRUE

Difficulty: Advanced

RYour turn
ex_6_4 <- # your code here file.exists(ex_6_4)

Click to reveal solution
RSolution
out_path <- tempfile(fileext = ".html") gtsave(ex_6_3, filename = out_path) ex_6_4 <- out_path file.exists(ex_6_4) #> [1] TRUE

Explanation: gtsave() infers the export format from the extension: .html writes a self-contained page (CSS inlined), .png/.pdf shell out to webshot2/Chromium, .tex/.rtf/.docx go through dedicated converters. HTML is the lossless default; PNG is great for slide decks but rasterised, and PDF requires a working headless Chrome. For automated pipelines, gtsave to HTML and then post-process with pagedown::chrome_print() if you need a deterministic PDF.

What to do next