rtabulator provides R bindings for Tabulator JS.
The goal of rtabulator is to make it easy to create elegant and interactive tables in markdown documents and Shiny applications. Furthermore, it supports creating spreadsheets with multiple sheets.
Basic usage
To render a table just pass a data frame to
tabulator()
:
tabulator(airquality)
In this case, the column definitions are automatically created for you. The horizontal alignment for character columns is set to left while numeric columns are aligned right.
It is also possible to pass a data URL:
Setup options
With tabulator_options()
you can customize your table
(or spreadsheet):
setup <- tabulator_options(
row_height = 50,
selectable_rows = FALSE,
edit_trigger_event = "click"
)
tabulator(iris, setup, editable = TRUE)
Pagination
setup <- tabulator_options(
pagination = TRUE,
pagination_size = 10,
pagination_size_selector = c(10, 20, 50)
)
tabulator(USArrests, setup)
Note
If you prefer the pipe style, you can also use the helper function
set_options_pagination()
.
Column formatters
To customize your columns, the easiest way is to use the
set_formatter_*()
functions:
tabulator(airquality) |>
set_formatter_progress("Temp", legend = TRUE, legend_align = "left") |>
set_formatter_traffic_light("Ozone") |>
set_tooltip("Ozone")
With for_each_col()
you can apply a formatter and any
other function that updates the column settings to multiple columns:
numeric_cols <- c("Sepal_Length", "Sepal_Width", "Petal_Length", "Petal_Width")
tabulator(iris) |>
for_each_col(
numeric_cols,
set_formatter_progress,
legend = TRUE,
legend_align = "left",
min = 0,
max = 10,
color = c("yellow", "orange", "green")
) |>
for_each_col(numeric_cols, set_tooltip)
Multi column headers
You can create multi column headers with
set_multi_column_header()
:
headers <- list(
Sepal = c("Sepal_Width", "Sepal_Length"),
Petal = c("Petal_Width", "Petal_Length")
)
tabulator(iris) |>
set_header_filter("Species", "list", placeholder = "Select Species") |>
set_multi_column_header(headers)
Note
If further functions update the column definitions,
set_multi_column_header()
must be the last one. Therefore,
in the example above set_header_filter()
is called before
we set the multi column header.
Groupings
setup <- tabulator_options(
group_by = c("Pclass"),
group_start_open = FALSE,
group_toggle_element = "header"
)
titanic_df <- titanic(c("Pclass", "Sex", "Age", "Survived"))
tabulator(titanic_df, setup) |>
set_formatter_traffic_light("Survived", color = c("red", "green"))
Multi level grouping is done by passing a vector as
group_by
parameter:
setup <- tabulator_options(
group_by = c("Sex", "Pclass"),
group_start_open = c(TRUE, FALSE)
)
tabulator(titanic_df, setup)
Note
You can also use set_options_group_by()
to set the group
by options.
Calculations
With set_calculation()
you can add calculations to the
top or bottom of columns:
tabulator(iris[4:5]) |>
set_calculation("Petal_Width", "avg", pos = "top") |>
set_calculation("Petal_Width", "max", precision = 1, pos = "bottom")
Editors
Set editable = TRUE
to make all columns of the table
editable. In this case, the editor for numeric columns is set to
number
and all others to input
.
setup <- tabulator_options(edit_trigger_event = "click")
tabulator(iris, setup, editable = TRUE)
To customize editor settings and validate user input use
set_editor()
:
tabulator(iris[, c(2, 5)], setup) |>
set_editor(
column = "Species",
editor = "list",
values_lookup = "active",
clearable = TRUE,
autocomplete = TRUE
) |>
set_editor(
column = "Sepal_Width",
editor = "number",
min = 0,
max = 10,
step = 0.1,
validator = c("min:0", "max:10")
)
Spreadsheets
To create an empty spreadsheet just pass an empty list as
data
parameter and spreadsheet = TRUE
to
tabulator()
:
The data format for spreadsheets is a list of lists or vectors where each entry represents a row in the spreadsheet:
To use multiple sheets pass a list of sheets created
withspreadsheet_def()
. In this case, data is passed with
the data
parameter of the sheet definitions:
sheets <- list(
spreadsheet_def(title = "Lee", data = spreadsheet_data),
spreadsheet_def(title = "Morgan", data = list()) # Empty spreadsheet
)
setup <- tabulator_options(
spreadsheet = TRUE,
spreadsheet_sheets = sheets,
spreadsheet_sheet_tabs = TRUE,
edit_trigger_event = "click"
)
tabulator(NULL, setup)