| lt_interactive | R Documentation |
Make a table interactive in the browser: pagination, a table-wide search box, click-to-sort column headers, per-column filter boxes, and resizable columns. The features are handled client-side by an opt-in JavaScript extension, loaded only for tables that call this function.
lt_interactive(
x,
sort = TRUE,
search = TRUE,
filter = FALSE,
pager = c(10, 25, 50, 100),
resize = FALSE,
hide = NULL,
detail = NULL,
download = FALSE
)
x |
An |
sort |
Whether clicking a column header sorts the table by that column
(cycling ascending, descending, then unsorted); shift-clicking adds a
column as a further tie-breaker, so several columns can sort at once.
Sorting uses the raw values, so numeric columns sort numerically. Can also
request an initial sort by one or more columns, given either as a character
vector of column names or as a one-sided formula (e.g. |
search |
Whether to show a table-wide search box. A row is kept when
any cell matches the term. Besides plain substring matching, a term that
references the cell variable |
filter |
The per-column filters. |
pager |
The page sizes to offer, as a vector of row counts; the
first one is used initially. Paging shows that many of the filtered and
sorted rows at a time, with a pager (first, previous, next, last), the row
range, and (for more than one size) a selector below the table. |
resize |
Whether to let the reader drag a column's right edge to resize
it (double-clicking the edge fits the column to its content). Widening a
column widens the table, leaving the other columns as they are. Initial
widths can be set with |
hide |
Column-visibility control: an eye button at the start of the
search row opens a checklist of every column, where unchecking a column
hides it (header and cells) and re-checking restores it. |
detail |
Row detail (drill-down). An expand caret is added to each row;
clicking it reveals a table built from that row below it. Give the columns
to show, as a character vector of names or a one-sided formula (e.g.
|
download |
Whether to add a button that downloads the table as a CSV
file. The file holds the current view (every row the filters and search
keep, in sort order, across all pages) of the table's columns. |
Sorting and filtering respect the row structure, happening within each
group or subtree rather than across it. Separator row groups (lt_group()
with sep = TRUE, or manual groups) keep their order, and a group whose rows
all filter out drops its header. Indentation (lt_indent()) sorts siblings
under each parent, and an indented child keeps its ancestors visible for
context. Rowspan row groups (lt_group() with its default rowspan rendering)
sort and filter within each run and clicking a group header reorders its
blocks; with several group columns the view stays nested (an inner column
reorders only within its parent block), and the spanning cells are redrawn to
match.
x with interactivity enabled.
lt(head(mtcars)) |> lt_interactive()
# search only, no click-to-sort or pagination
lt(head(mtcars)) |> lt_interactive(sort = FALSE, pager = FALSE)
# 5 rows at a time, or all of them, with a filter box on one column
lt(mtcars) |> lt_interactive(filter = 'cyl', pager = c(5, Inf))
# resizable columns
lt(head(mtcars)) |> lt_interactive(resize = TRUE)
# a column-visibility menu (all columns shown, or with some hidden to start)
lt(head(mtcars)) |> lt_interactive(hide = TRUE)
lt(head(mtcars)) |> lt_interactive(hide = ~ hp + drat)
# an initial sort by cyl, then mpg descending within each
lt(mtcars) |> lt_interactive(sort = ~ cyl + -mpg)
# expandable row detail (drill-down): name the columns to show. Here gear and
# carb are hidden from the main table but revealed in the detail that appears
# when a row is expanded.
lt(head(mtcars)) |>
lt_hide(~ gear + carb) |>
lt_interactive(detail = ~ gear + carb)
# for full control, pass a JavaScript callback yourself: (raw row, index,
# displayed row); here showing the row number and mpg as the table displays it
lt(head(mtcars)) |> lt_interactive(
detail = js('(row, i, d) => ({ data: { metric: ["row", "mpg"],
value: [i, d.mpg] } })')
)
# a button to download the current view as a CSV file
lt(head(mtcars)) |> lt_interactive(download = TRUE)
lt(head(mtcars)) |> lt_interactive(download = 'cars.csv')
# export the raw (unformatted) values instead of the displayed text
lt(head(mtcars)) |> lt_interactive(download = list(file = 'cars.csv', mode = 'raw'))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.