The tab_options() function

Use select columns from the exibble dataset to create a gt table with a number of table parts added (using functions like summary_rows(), grand_summary_rows(), and more). We can use this gt object going forward to demo some of tab_options() features.

tab_1 <-
  exibble |>
  dplyr::select(-c(fctr, date, time, datetime)) |>
  gt(
    rowname_col = "row",
    groupname_col = "group"
  ) |>
  tab_header(
    title = md("Data listing from **exibble**"),
    subtitle = md("`exibble` is an R dataset")
  ) |>
  fmt_number(columns = num) |>
  fmt_currency(columns = currency) |>
  tab_footnote(
    footnote = "Using commas for separators.",
    locations = cells_body(
      columns = num,
      rows = num > 1000
    )
  ) |>
  tab_footnote(
    footnote = "Using commas for separators.",
    locations = cells_body(
      columns = currency,
      rows = currency > 1000
    )
  ) |>
  tab_footnote(
    footnote = "Alphabetical fruit.",
    locations = cells_column_labels(columns = char)
  )

tab_1
Data listing from exibble
exibble is an R dataset
num char1 currency
grp_a
row_1 0.11 apricot $49.95
row_2 2.22 banana $17.95
row_3 33.33 coconut $1.39
row_4 444.40 durian 2 $65,100.00
grp_b
row_5 2 5,550.00 NA 2 $1,325.81
row_6 NA fig $13.26
row_7 2 777,000.00 grapefruit NA
row_8 2 8,880,000.00 honeydew $0.44
1 Alphabetical fruit.
2 Using commas for separators.

We can modify the table width to be set as ‘100%’. In effect, this spans the table to entirely fill the content width area. This is done with the table.width option and we take advantage of the pct() helper function.

tab_1 |> tab_options(table.width = pct(100))
Data listing from exibble
exibble is an R dataset
num char1 currency
grp_a
row_1 0.11 apricot $49.95
row_2 2.22 banana $17.95
row_3 33.33 coconut $1.39
row_4 444.40 durian 2 $65,100.00
grp_b
row_5 2 5,550.00 NA 2 $1,325.81
row_6 NA fig $13.26
row_7 2 777,000.00 grapefruit NA
row_8 2 8,880,000.00 honeydew $0.44
1 Alphabetical fruit.
2 Using commas for separators.

With the table.background.color option, we can modify the table’s background color. Here, we want that to be "lightcyan".

tab_1 |> tab_options(table.background.color = "lightcyan")
Data listing from exibble
exibble is an R dataset
num char1 currency
grp_a
row_1 0.11 apricot $49.95
row_2 2.22 banana $17.95
row_3 33.33 coconut $1.39
row_4 444.40 durian 2 $65,100.00
grp_b
row_5 2 5,550.00 NA 2 $1,325.81
row_6 NA fig $13.26
row_7 2 777,000.00 grapefruit NA
row_8 2 8,880,000.00 honeydew $0.44
1 Alphabetical fruit.
2 Using commas for separators.

We have footnotes residing in the footer section of tab_1. By default, each footnote takes up a new line of text. This can be changed with the footnotes.multiline option. Using FALSE with that means that all footnotes will be placed into one continuous span of text. Speaking of footnotes, we can change the ‘marks’ used to identify them. Here, we’ll use letters as the marks for footnote references (instead of the default, which is numbers). This is accomplished with the footnotes.marks option, and we are going to supply the letters vector to that.

tab_1 |>
  tab_options(
    footnotes.multiline = FALSE,
    footnotes.marks = letters
  )
Data listing from exibble
exibble is an R dataset
num chara currency
grp_a
row_1 0.11 apricot $49.95
row_2 2.22 banana $17.95
row_3 33.33 coconut $1.39
row_4 444.40 durian b $65,100.00
grp_b
row_5 b 5,550.00 NA b $1,325.81
row_6 NA fig $13.26
row_7 b 777,000.00 grapefruit NA
row_8 b 8,880,000.00 honeydew $0.44
a Alphabetical fruit. b Using commas for separators.

The data rows of a table typically take up the most physical space but we have some control over the extent of that. With the data_row.padding option, it’s possible to modify the top and bottom padding of data rows. We’ll do just that in the following example, reducing the padding to a value of 5 px (note that we are using the px() helper function here).

tab_1 |> tab_options(data_row.padding = px(5))
Data listing from exibble
exibble is an R dataset
num char1 currency
grp_a
row_1 0.11 apricot $49.95
row_2 2.22 banana $17.95
row_3 33.33 coconut $1.39
row_4 444.40 durian 2 $65,100.00
grp_b
row_5 2 5,550.00 NA 2 $1,325.81
row_6 NA fig $13.26
row_7 2 777,000.00 grapefruit NA
row_8 2 8,880,000.00 honeydew $0.44
1 Alphabetical fruit.
2 Using commas for separators.

The size of the title and the subtitle text in the header of the table can be altered with the heading.title.font.size and heading.subtitle.font.size options. Here, we’ll use the "small" keyword as a value for both options.

tab_1 |>
  tab_options(
    heading.title.font.size = "small",
    heading.subtitle.font.size = "small"
  )
Data listing from exibble
exibble is an R dataset
num char1 currency
grp_a
row_1 0.11 apricot $49.95
row_2 2.22 banana $17.95
row_3 33.33 coconut $1.39
row_4 444.40 durian 2 $65,100.00
grp_b
row_5 2 5,550.00 NA 2 $1,325.81
row_6 NA fig $13.26
row_7 2 777,000.00 grapefruit NA
row_8 2 8,880,000.00 honeydew $0.44
1 Alphabetical fruit.
2 Using commas for separators.