By default, a gt table rendered as HTML will essentially be a 'static'
table. However, we can make it 'interactive' and configure those interactive
HTML options through the opt_interactive() function. Making an HTML table
interactive entails the enabling of controls for pagination, global search,
filtering, sorting, and more.
This function serves as a shortcut for setting the following options in
tab_options():
ihtml.activeihtml.use_paginationihtml.use_pagination_infoihtml.use_sortingihtml.use_searchihtml.use_filtersihtml.use_resizersihtml.use_highlightihtml.use_compact_modeihtml.use_page_size_selectihtml.page_size_defaultihtml.page_size_valuesihtml.pagination_typeihtml.heightihtml.selection_mode
Usage
opt_interactive(
data,
active = TRUE,
use_pagination = TRUE,
use_pagination_info = TRUE,
use_sorting = TRUE,
use_search = FALSE,
use_filters = FALSE,
use_resizers = FALSE,
use_highlight = FALSE,
use_compact_mode = FALSE,
use_text_wrapping = TRUE,
use_page_size_select = FALSE,
page_size_default = 10,
page_size_values = c(10, 25, 50, 100),
pagination_type = c("numbers", "jump", "simple"),
height = "auto",
selection_mode = NULL
)Arguments
- data
The gt table data object
obj:<gt_tbl>// requiredThis is the gt table object that is commonly created through use of the
gt()function.- active
Display interactive HTML table
scalar<logical>// default:TRUEThe
activeoption will either enable or disable interactive features for an HTML table. The individual features of an interactive HTML table are controlled by the other options.- use_pagination
Display pagination controls
scalar<logical>// default:TRUEThis is the option for using pagination controls (below the table body). By default, this is
TRUEand it will allow the use to page through table content.- use_pagination_info
Display pagination info
scalar<logical>// default:TRUEIf
use_paginationisTRUEthen theuse_pagination_infooption can be used to display informational text regarding the current page view (this is set toTRUEby default).- use_sorting
Provide column sorting controls
scalar<logical>// default:TRUEThis option provides controls for sorting column values. By default, this is
TRUE.- use_search
Provide a global search field
scalar<logical>// default:FALSEThe
use_searchoption places a search field for globally filtering rows to the requested content. By default, this isFALSE.- use_filters
Display filtering fields
scalar<logical>// default:FALSEThe
use_filtersoption places search fields below each column header and allows for filtering by column. By default, this isFALSE.- use_resizers
Allow column resizing
scalar<logical>// default:FALSEThis option allows for the interactive resizing of columns. By default, this is
FALSE.- use_highlight
Enable row highlighting on hover
scalar<logical>// default:FALSEThe
use_highlightoption highlights individual rows upon hover. By default, this isFALSE.- use_compact_mode
Use compact mode
scalar<logical>// default:FALSETo reduce vertical padding and thus make the table consume less vertical space the
use_compact_modeoption can be used. By default, this isFALSE.- use_text_wrapping
Use text wrapping
scalar<logical>// default:TRUEThe
use_text_wrappingoption controls whether text wrapping occurs throughout the table. This isTRUEby default and with that text will be wrapped to multiple lines. IfFALSE, text will be truncated to a single line.- use_page_size_select
Allow for page size selection
scalar<logical>// default:FALSEThe
use_page_size_selectoption lets us display a dropdown menu for the number of rows to show per page of data.- page_size_default
Change the default page size
scalar<numeric|integer>// default:10The default page size (initially set as
10) can be modified withpage_size_defaultand this works whether or notuse_page_size_selectis set toTRUE.- page_size_values
Set of page-size values
vector<numeric|integer>// default:c(10, 25, 50, 100)By default, this is the vector
c(10, 25, 50, 100)which corresponds to options for10,25,50, and100rows of data per page. To modify these page-size options, provide a numeric vector topage_size_values.- pagination_type
Change pagination mode
scalar<character>// default:"numbers"When using pagination the
pagination_typeoption lets us select between one of three options for the layout of pagination controls. The default is"numbers", where a series of page-number buttons is presented along with 'previous' and 'next' buttons. The"jump"option provides an input field with a stepper for the page number. With"simple", only the 'previous' and 'next' buttons are displayed.- height
Height of interactive HTML table
Height of the table in pixels. Defaults to
"auto"for automatic sizing.- selection_mode
Allow row selection
scalar<character>// default:NULLThe
selection_modeoptions allows users to select rows by clicking them. When this option is"single", clicking another value toggles selection of the previously selected row off. When this option is"multiple", multiple rows can be selected at once. Selected values are available in Shiny apps whenselection_modeis notNULLand the table is used inrender_gt().
Examples
Use select columns from the towny dataset to create a gt table with a
header (through tab_header()) and a source note (through
tab_source_note()). Next, we will add interactive HTML features (and
otherwise activate interactive HTML mode) 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()
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()
See also
Other table option functions:
opt_align_table_header(),
opt_all_caps(),
opt_css(),
opt_footnote_marks(),
opt_footnote_order(),
opt_footnote_spec(),
opt_horizontal_padding(),
opt_row_striping(),
opt_stylize(),
opt_table_font(),
opt_table_lines(),
opt_table_outline(),
opt_vertical_padding()