| dt2 | R Documentation |
The main function for creating interactive DataTables. Works standalone (R Markdown, Quarto, Viewer) and inside Shiny.
Styling is controlled directly via theme, striped, hover,
compact, font_scale – or a CSS class string for full control.
DataTables configuration goes in options (1:1 mapping to the
JavaScript API). The two concerns are cleanly separated.
dt2(
data,
theme = "default",
striped = NULL,
hover = NULL,
compact = NULL,
font_scale = NULL,
style = NULL,
class = NULL,
button_class = NULL,
responsive = TRUE,
options = list(),
extensions = NULL,
width = "100%",
height = NULL,
elementId = NULL
)
data |
A |
theme |
A theme preset name ( |
striped, hover, compact |
Logical; override the theme.
|
font_scale |
Numeric; override the theme font-scale.
|
style |
Styling framework: |
class |
Optional CSS class string (e.g., |
button_class |
CSS class for Buttons extension buttons.
Default: |
responsive |
Logical; enable the Responsive extension so the table
fills 100\
Default: |
options |
List of DataTables options. See https://datatables.net/reference/option/. |
extensions |
Character vector of extensions to load
(e.g., |
width, height |
CSS dimensions. |
elementId |
Optional HTML element ID. |
An htmlwidget object.
# Just works — beautiful defaults
dt2(iris)
# Override style inline
dt2(iris, striped = FALSE)
dt2(iris, font_scale = 0.85, compact = FALSE)
# Theme presets
dt2(iris, theme = "minimal")
dt2(iris, theme = "compact")
# Reusable theme
my_theme <- dt2_theme("clean", compact = TRUE)
dt2(iris, theme = my_theme)
# Override a preset
dt2(iris, theme = "minimal", striped = TRUE)
# CSS class override (power users)
dt2(iris, class = "table table-bordered table-dark")
# DataTables options (separate from styling)
dt2(iris, options = list(pageLength = 5, searching = FALSE))
# Disable responsive (fixed-width columns)
dt2(iris, responsive = FALSE)
# Everything composes
dt2(mtcars,
theme = "clean",
compact = TRUE,
options = list(pageLength = 25))
# Buttons
dt2(mtcars, options = list(
buttons = list("copy", "csv", "excel"),
layout = list(topEnd = "buttons")
))
# Custom button style
dt2(mtcars,
button_class = "btn btn-sm btn-primary",
options = list(
buttons = list("copy", "csv", "excel"),
layout = list(topEnd = "buttons")
))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.