The opt_interactive() function

Use the towny dataset to create a gt table with a header and a source note. Next, we add interactive HTML features through opt_interactive(). It’ll just be the default set of interactive options.

towny |>
  dplyr::select(name, census_div, starts_with("population")) |>
  gt() |>
  fmt_integer() |>
  cols_label_with(fn = function(x) sub("population_", "", x)) |>
  cols_width(
    name ~ px(200),
    census_div ~ px(200)
  ) |>
  tab_header(
    title = "Populations of Municipalities",
    subtitle = "Census values from 1996 to 2021."
  ) |>
  tab_source_note(source_note = md("Data taken from the `towny` dataset.")) |>
  opt_interactive()
Populations of Municipalities
Census values from 1996 to 2021.
name
census_div
1996
2001
2006
2011
2016
2021
Addington Highlands
Lennox and Addington
2,429
2,402
2,512
2,517
2,318
2,534
Adelaide Metcalfe
Middlesex
3,128
3,149
3,135
3,028
2,990
3,011
Adjala-Tosorontio
Simcoe
9,359
10,082
10,695
10,603
10,975
10,989
Admaston/Bromley
Renfrew
2,837
2,824
2,716
2,844
2,935
2,995
Ajax
Durham
64,430
73,753
90,167
109,600
119,677
126,666
Alberton
Rainy River
1,027
956
958
864
969
954
Alfred and Plantagenet
Prescott and Russell
8,315
8,593
8,654
9,196
9,680
9,949
Algonquin Highlands
Haliburton
1,687
1,827
1,976
2,186
2,351
2,588
Alnwick/Haldimand
Northumberland
5,528
5,846
6,435
6,617
6,869
7,473
Amaranth
Dufferin
3,450
3,770
3,845
3,963
4,079
4,327
1–10 of 414 rows
...
Data taken from the towny dataset.

Interactive tables can have styled body cells. Here, we use the gtcars dataset to create an interactive gt table. Using tab_style() and data_color() we can flexibly style body cells throughout the table.

gtcars |>
  gt() |>
  cols_width(everything() ~ px(130)) |>
  tab_style(
    style = cell_fill(color = "gray95"),
    locations = cells_body(columns = c(mfr, model))
  ) |>
  data_color(
    columns = c(starts_with("hp"), starts_with("trq")),
    method = "numeric",
    palette = "viridis"
  ) |>
  cols_hide(columns = trim) |>
  opt_interactive()
mfr
model
year
bdy_style
hp
hp_rpm
trq
trq_rpm
mpg_c
mpg_h
drivetrain
trsmn
ctry_origin
msrp
Ford
GT
2017
coupe
647
6250
550
5900
11
18
rwd
7a
United States
447000
Ferrari
458 Speciale
2015
coupe
597
9000
398
6000
13
17
rwd
7a
Italy
291744
Ferrari
458 Spider
2015
convertible
562
9000
398
6000
13
17
rwd
7a
Italy
263553
Ferrari
458 Italia
2014
coupe
562
9000
398
6000
13
17
rwd
7a
Italy
233509
Ferrari
488 GTB
2016
coupe
661
8000
561
3000
15
22
rwd
7a
Italy
245400
Ferrari
California
2015
convertible
553
7500
557
4750
16
23
rwd
7a
Italy
198973
Ferrari
GTC4Lusso
2017
coupe
680
8250
514
5750
12
17
awd
7a
Italy
298000
Ferrari
FF
2015
coupe
652
8000
504
6000
11
16
awd
7a
Italy
295000
Ferrari
F12Berlinetta
2015
coupe
731
8250
509
6000
11
16
rwd
7a
Italy
319995
Ferrari
LaFerrari
2015
coupe
949
9000
664
6750
12
16
rwd
7a
Italy
1416362
1–10 of 47 rows