The cells_column_spanners() function

Use the exibble dataset to create a gt table. We’ll add a spanner column label over three columns (date, time, and datetime) with tab_spanner(). The spanner column label can be styled with tab_style() by using the cells_column_spanners() function in locations. In this example, we are making the text of the column spanner label appear as bold.

exibble |>
  dplyr::select(-fctr, -currency, -group) |>
  gt(rowname_col = "row") |>
  tab_spanner(
    label = "dates and times",
    columns = c(date, time, datetime),
    id = "dt"
  ) |>
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_column_spanners(spanners = "dt")
  )
num char
dates and times
date time datetime
row_1 1.111e-01 apricot 2015-01-15 13:35 2018-01-01 02:22
row_2 2.222e+00 banana 2015-02-15 14:40 2018-02-02 14:33
row_3 3.333e+01 coconut 2015-03-15 15:45 2018-03-03 03:44
row_4 4.444e+02 durian 2015-04-15 16:50 2018-04-04 15:55
row_5 5.550e+03 NA 2015-05-15 17:55 2018-05-05 04:00
row_6 NA fig 2015-06-15 NA 2018-06-06 16:11
row_7 7.770e+05 grapefruit NA 19:10 2018-07-07 05:22
row_8 8.880e+06 honeydew 2015-08-15 20:20 NA

Use the exibble dataset to create a gt table. We’ll add two spanners for the column combinations of (num, char) and time related columns (time and datetime). Furthermore we add another level of spanners with a column label over all date- and time related columns (date, time, and datetime). We want all spanner labels with “time” in their name to be bold. Additionally we want the text to be red of the spanner that is both time- related and on level 1.

exibble |>
  dplyr::select(-fctr, -currency, -group) |>
  gt(rowname_col = "row") |>
  tab_spanner(
    label = "time related cols",
    columns = c(datetime, time)
  ) |>
  tab_spanner(
    label = "num and char",
    columns = c(num, char)
  ) |>
  tab_spanner(
    label = "date and time cols",
    columns = c(date, time, datetime)
  ) |>
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_column_spanners(spanners = tidyselect::contains("time"))
  ) |>
  tab_style(
    style = cell_text(color = "red"),
    locations = cells_column_spanners(
      spanners = tidyselect::contains("time"),
      levels = 1
    )
  )
date and time cols
num and char
date
time related cols
num char datetime time
row_1 1.111e-01 apricot 2015-01-15 2018-01-01 02:22 13:35
row_2 2.222e+00 banana 2015-02-15 2018-02-02 14:33 14:40
row_3 3.333e+01 coconut 2015-03-15 2018-03-03 03:44 15:45
row_4 4.444e+02 durian 2015-04-15 2018-04-04 15:55 16:50
row_5 5.550e+03 NA 2015-05-15 2018-05-05 04:00 17:55
row_6 NA fig 2015-06-15 2018-06-06 16:11 NA
row_7 7.770e+05 grapefruit NA 2018-07-07 05:22 19:10
row_8 8.880e+06 honeydew 2015-08-15 NA 20:20