dt2: Create a DT2 DataTable widget

View source: R/dt2.R

dt2R Documentation

Create a DT2 DataTable widget

Description

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.

Usage

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
)

Arguments

data

A data.frame, tibble, or matrix.

theme

A theme preset name ("default", "clean", "minimal", "compact") or a dt2_theme() object. Default: "default".

striped, hover, compact

Logical; override the theme. NULL (default) = use theme value.

font_scale

Numeric; override the theme font-scale. NULL (default) = use theme value.

style

Styling framework: "bootstrap5" (default) or "core".

class

Optional CSS class string (e.g., "table table-dark"). If provided, overrides all theme-generated classes.

button_class

CSS class for Buttons extension buttons. Default: "btn btn-sm btn-outline-secondary". Examples: "btn btn-sm btn-primary", "btn btn-sm btn-outline-dark".

responsive

Logical; enable the Responsive extension so the table fills 100\ Default: TRUE. Set FALSE to disable.

options

List of DataTables options. See https://datatables.net/reference/option/.

extensions

Character vector of extensions to load (e.g., c("Buttons", "Select")). Auto-detected from options when NULL.

width, height

CSS dimensions.

elementId

Optional HTML element ID.

Value

An htmlwidget object.

Examples

# 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")
    ))


DT2 documentation built on June 14, 2026, 9:06 a.m.