Skip to contents
Code

You can find visual tests for LaTeX

Background color

Code
tab <- exibble |>
  gt() |>
  tab_options(
    column_labels.background.color = "gray"
  ) |>
  tab_spanner(c(num, char), label = "A spanner") |>
  tab_header(title = "background color")
background color
A spanner
num
char
fctr
date
time
datetime
currency
row
group
0.1111
apricot
one
2015-01-15
13:35
2018-01-01 02:22
49.95
row_1
grp_a
2.222
banana
two
2015-02-15
14:40
2018-02-02 14:33
17.95
row_2
grp_a
33.33
coconut
three
2015-03-15
15:45
2018-03-03 03:44
1.39
row_3
grp_a
444.4
durian
four
2015-04-15
16:50
2018-04-04 15:55
65100
row_4
grp_a
5550
NA
five
2015-05-15
17:55
2018-05-05 04:00
1325.81
row_5
grp_b
NA
fig
six
2015-06-15
NA
2018-06-06 16:11
13.255
row_6
grp_b
777000
grapefruit
seven
NA
19:10
2018-07-07 05:22
NA
row_7
grp_b
8880000
honeydew
eight
2015-08-15
20:20
NA
0.44
row_8
grp_b
1–8 of 8 rows

Spanner and Stubhead

Code
tab <- exibble |>
  gt(rowname_col = "row", groupname_col = "group", row_group_as_column = TRUE) |>
  tab_spanner("spanners", c(char, num)) |>
  tab_spanner("Second level spanners", c(char, num, fctr)) |>
  tab_stubhead("Stubhead label") |>
  tab_style(
    style = list(cell_fill("#f0f0f0"), cell_text(weight = "bold", size = "small", color = "blue")),
    cells_stubhead()
  ) |> 
  tab_header(title = "Stubhead styling and multiple spanner levels")
Stubhead styling and multiple spanner levels
spanners
Stubhead label
char
num
fctr
date
time
datetime
currency
grp_a
row_1
apricot
0.1111
one
2015-01-15
13:35
2018-01-01 02:22
49.95
row_2
banana
2.222
two
2015-02-15
14:40
2018-02-02 14:33
17.95
row_3
coconut
33.33
three
2015-03-15
15:45
2018-03-03 03:44
1.39
row_4
durian
444.4
four
2015-04-15
16:50
2018-04-04 15:55
65100
grp_b
row_5
NA
5550
five
2015-05-15
17:55
2018-05-05 04:00
1325.81
row_6
fig
NA
six
2015-06-15
NA
2018-06-06 16:11
13.255
row_7
grapefruit
777000
seven
NA
19:10
2018-07-07 05:22
NA
row_8
honeydew
8880000
eight
2015-08-15
20:20
NA
0.44
1–2 of 2 rows

Complete output

Code
tab <- pizzaplace |>
  dplyr::filter(type %in% c("classic", "veggie")) |>
  dplyr::group_by(type, size) |>
  dplyr::summarize(
    sold = dplyr::n(),
    income = sum(price),
    .groups = "drop"
  ) |>
  gt(rowname_col = "size", groupname_col = "type") |>
  tab_header(title = "Complete output") |>
  fmt_integer(columns = sold) |>
  fmt_currency(columns = income) |>
  summary_rows(
    fns = list(label = "All Sizes", fn = "sum"),
    side = c("top"),
    fmt = list(
      ~ fmt_integer(., columns = sold),
      ~ fmt_currency(., columns = income)
    )
  ) |>
  tab_options(
    summary_row.background.color = "gray95",
    row_group.as_column = TRUE
  ) |>
  tab_stub_indent(
    rows = everything(),
    indent = 2
  ) |>
  grand_summary_rows(
    columns = c("sold", "income"),
    fns = list(Sum ~ sum(.)),
    fmt = ~ fmt_number(.)
  ) |>
  tab_caption("Here be caption text") |>
  tab_spanner(
    label = "Spanner",
    columns = c("sold", "income")
  ) |>
  tab_stubhead("Stubhead label") |>
  tab_source_note("Source: the pizzaria") |>
  tab_footnote("Pineapples not included")
Complete output
Spanner
Stubhead label
sold
income
classic
L...1
4,057
$74,518.50
M...2
4,112
$60,581.75
S...3
6,139
$69,870.25
XL
552
$14,076.00
XXL
28
$1,006.60
veggie
L...6
5,403
$104,202.70
M...7
3,583
$57,101.00
S...8
2,663
$32,386.75
1–2 of 2 rows
Source: the pizzaria