The extract_body() function

Use a modified version of sp500 the dataset to create a gt table with row groups and row labels. Formatting will be applied to the date- and currency-based columns.

gt_tbl <-
  sp500 |>
  dplyr::filter(date >= "2015-01-05" & date <= "2015-01-16") |>
  dplyr::arrange(date) |>
  dplyr::mutate(week = paste0("W", strftime(date, format = "%V"))) |>
  dplyr::select(-adj_close, -volume) |>
  gt(
    rowname_col = "date",
    groupname_col = "week"
  ) |>
  fmt_date(columns = date, date_style = "day_month_year") |>
  fmt_currency(columns = c(open, high, low, close)) |>
  cols_hide(columns = c(high, low))
gt_tbl
open close
W02
5 January 2015 $2,054.44 $2,020.58
6 January 2015 $2,022.15 $2,002.61
7 January 2015 $2,005.55 $2,025.90
8 January 2015 $2,030.61 $2,062.14
9 January 2015 $2,063.45 $2,044.81
W03
12 January 2015 $2,046.13 $2,028.26
13 January 2015 $2,031.58 $2,023.03
14 January 2015 $2,018.40 $2,011.27
15 January 2015 $2,013.75 $1,992.67
16 January 2015 $1,992.25 $2,019.42

Using extract_body() on the gt object (gt_tbl) will provide us with a tibble that contains the fully built data cells for the output context (in this case, "html").

extract_body(gt_tbl)
# A tibble: 10 × 4
   `::group_id::` `::rowname::`   open      close    
   <chr>          <chr>           <chr>     <chr>    
 1 W02            5 January 2015  $2,054.44 $2,020.58
 2 W02            6 January 2015  $2,022.15 $2,002.61
 3 W02            7 January 2015  $2,005.55 $2,025.90
 4 W02            8 January 2015  $2,030.61 $2,062.14
 5 W02            9 January 2015  $2,063.45 $2,044.81
 6 W03            12 January 2015 $2,046.13 $2,028.26
 7 W03            13 January 2015 $2,031.58 $2,023.03
 8 W03            14 January 2015 $2,018.40 $2,011.27
 9 W03            15 January 2015 $2,013.75 $1,992.67
10 W03            16 January 2015 $1,992.25 $2,019.42

To provide us with a better frame of reference, the grouping and row label values are provided as the first columns in the returned output. We could suppress those in the output by setting incl_stub_cols = FALSE.

extract_body(gt_tbl, incl_stub_cols = FALSE)
# A tibble: 10 × 2
   open      close    
   <chr>     <chr>    
 1 $2,054.44 $2,020.58
 2 $2,022.15 $2,002.61
 3 $2,005.55 $2,025.90
 4 $2,030.61 $2,062.14
 5 $2,063.45 $2,044.81
 6 $2,046.13 $2,028.26
 7 $2,031.58 $2,023.03
 8 $2,018.40 $2,011.27
 9 $2,013.75 $1,992.67
10 $1,992.25 $2,019.42

The high and low columns were hidden via cols_hide() and so they won’t be shown in the returned data unless we use incl_hidden_cols = TRUE.

extract_body(
  gt_tbl,
  incl_stub_cols = FALSE,
  incl_hidden_cols = TRUE
)
# A tibble: 10 × 4
   open      high      low       close    
   <chr>     <chr>     <chr>     <chr>    
 1 $2,054.44 $2,054.44 $2,017.34 $2,020.58
 2 $2,022.15 $2,030.25 $1,992.44 $2,002.61
 3 $2,005.55 $2,029.61 $2,005.55 $2,025.90
 4 $2,030.61 $2,064.08 $2,030.61 $2,062.14
 5 $2,063.45 $2,064.43 $2,038.33 $2,044.81
 6 $2,046.13 $2,049.30 $2,022.58 $2,028.26
 7 $2,031.58 $2,056.93 $2,008.25 $2,023.03
 8 $2,018.40 $2,018.40 $1,988.44 $2,011.27
 9 $2,013.75 $2,021.35 $1,991.47 $1,992.67
10 $1,992.25 $2,020.46 $1,988.12 $2,019.42