| Title: | 'DataTables' 2.x for R |
|---|---|
| Description: | A modern R binding for 'DataTables' V2 with modular extension loading, 'Bootstrap 5' styling, 'Shiny' integration (proxy, events, inline inputs), server-side processing helpers, and standalone (non-Shiny) support. Configure 'DataTables' options directly via R lists, a 1:1 mapping to the 'JavaScript' API. |
| Authors: | Andre Leite [aut, cre], Marcos Wasilew [aut], Hugo Vasconcelos [aut], Carlos Amorin [aut], Diogo Bezerra [aut] |
| Maintainer: | Andre Leite <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1 |
| Built: | 2026-06-04 07:49:06 UTC |
| Source: | https://github.com/strategicprojects/dt2 |
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 )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") ))# 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") ))
Bind a DataTables v2 server-side endpoint to a widget id
dt2_bind_server( id, data, session = shiny::getDefaultReactiveDomain(), handler = NULL )dt2_bind_server( id, data, session = shiny::getDefaultReactiveDomain(), handler = NULL )
id |
Output id of the widget (e.g., "tbl"). |
data |
A data.frame with the source data. |
session |
Shiny session (default: current). |
handler |
Optional custom handler function(data, req) -> list(...). |
No return value, called for side effects. Registers a Shiny
observer on session that responds to client-side server-processing
requests for the given widget id.
Configure DataTables Buttons and (optionally) move them to a custom container
dt2_buttons( options = list(), buttons = c("copyHtml5", "csvHtml5", "excelHtml5", "pdfHtml5", "print"), target = NULL )dt2_buttons( options = list(), buttons = c("copyHtml5", "csvHtml5", "excelHtml5", "pdfHtml5", "print"), target = NULL )
options |
A DT2 |
buttons |
Character vector with button names (e.g. |
target |
Optional CSS selector (e.g. |
Requires the Buttons extension. For CSV/Excel/PDF you also need JSZip and pdfMake (incl. vfs_fonts).
The modified options list.
Queries the npm registry to compare installed library versions against the latest available versions. Version constraints are enforced to prevent incompatible major version upgrades (e.g. jQuery 3.x will not jump to 4.x).
dt2_check_updates(quiet = FALSE)dt2_check_updates(quiet = FALSE)
quiet |
Logical. If |
A data.frame (invisibly) with columns:
library, installed, latest, latest_ok,
constraint, status.
Status values:
"ok"Library is up to date.
"UPDATE"A compatible update is available.
"PINNED"A new major version exists, but is blocked by the version constraint. The library is up to date within its allowed range.
"error"Lookup failed (check your internet connection).
dt2_check_updates() # programmatic use updates <- dt2_check_updates(quiet = TRUE) updates[updates$status == "UPDATE", ]dt2_check_updates() # programmatic use updates <- dt2_check_updates(quiet = TRUE) updates[updates$status == "UPDATE", ]
Action button per row
dt2_col_button( options = list(), col, label = "Action", input_id_prefix = "row_btn_" )dt2_col_button( options = list(), col, label = "Action", input_id_prefix = "row_btn_" )
options |
Options list. |
col |
Target column (name or 1-based index). |
label |
Button label. |
input_id_prefix |
Prefix for element ids (e.g., "row_btn_"). |
Updated options.
Checkbox input per row
dt2_col_checkbox( options = list(), col, input_id_prefix = "row_chk_", value_col = NULL )dt2_col_checkbox( options = list(), col, input_id_prefix = "row_chk_", value_col = NULL )
options |
Options list. |
col |
Target column (name or 1-based index). |
input_id_prefix |
Prefix for element ids (e.g., "row_chk_"). |
value_col |
Optional boolean column to define initial state. |
Updated options.
{{VAL}})Simple HTML template per column (replace {{VAL}})
dt2_col_template(options = list(), col, template)dt2_col_template(options = list(), col, template)
options |
Options list. |
col |
Name or index of target column. |
template |
HTML string with |
Updated options.
Column align (Bootstrap 5 classes)
dt2_cols_align(options = list(), cols, align = c("left", "center", "right"))dt2_cols_align(options = list(), cols, align = c("left", "center", "right"))
options |
Options list. |
cols |
Names or 1-based indices. |
align |
"left","center","right". |
Updated options.
Escape/unescape columns content
dt2_cols_escape(options = list(), cols, escape = TRUE)dt2_cols_escape(options = list(), cols, escape = TRUE)
options |
Options list. |
cols |
Names or indices. |
escape |
If FALSE, tells DT to trust HTML (use with care). |
Updated options.
Hide columns
dt2_cols_hide(options = list(), cols)dt2_cols_hide(options = list(), cols)
options |
Options list. |
cols |
Names or 1-based indices. |
Updated options.
columns.render
Mark columns to render raw HTML using a JS render function.
dt2_cols_html(options = list(), cols, js_render)dt2_cols_html(options = list(), cols, js_render)
options |
Options list. |
cols |
Names or 1-based indices. |
js_render |
JS function (via htmlwidgets::JS) with signature |
Updated options.
Provide a custom JS renderer for one or more columns. Use this when
you need fine control over columns.render, including returning
different outputs based on type (display/sort/filter/type).
dt2_cols_render_js(options = list(), col_specs, js_render)dt2_cols_render_js(options = list(), col_specs, js_render)
options |
List returned, with |
col_specs |
Column names or indices. |
js_render |
A |
Modified options.
https://datatables.net/reference/option/columns.render
Supply different renderers for each orthogonal data request. Pass an
object with keys display, sort, filter, type
(all optional). Each value must be a JS function.
dt2_cols_render_orthogonal( options = list(), col_specs, display = NULL, sort = NULL, filter = NULL, type = NULL )dt2_cols_render_orthogonal( options = list(), col_specs, display = NULL, sort = NULL, filter = NULL, type = NULL )
options |
Options list to modify. |
col_specs |
Column names or indices. |
display |
Optional JS renderer for UI display. |
sort |
Optional JS renderer used for ordering. |
filter |
Optional JS renderer used for searching. |
type |
Optional JS renderer used for type detection. |
Modified options.
opts <- list(columns = names(iris)) opts <- dt2_cols_render_orthogonal( opts, "Sepal.Length", display = htmlwidgets::JS("function(d,t,row,meta){ return d + ' cm'; }"), sort = htmlwidgets::JS("function(d,t,row,meta){ return parseFloat(d); }") )opts <- list(columns = names(iris)) opts <- dt2_cols_render_orthogonal( opts, "Sepal.Length", display = htmlwidgets::JS("function(d,t,row,meta){ return d + ' cm'; }"), sort = htmlwidgets::JS("function(d,t,row,meta){ return parseFloat(d); }") )
Column widths (CSS)
dt2_cols_width(options = list(), map_named)dt2_cols_width(options = list(), map_named)
options |
Options list. |
map_named |
Named character vector: c(Col="120px", ...). |
Updated options.
Redraw the table (proxy)
dt2_draw(proxy)dt2_draw(proxy)
proxy |
The proxy object, returned invisibly.
List available DataTables extensions
dt2_extensions()dt2_extensions()
A data.frame with columns name, version, dir.
dt2_extensions()dt2_extensions()
Use DataTables' built-in DataTable.render.datetime to transform
date/time strings for display (and preserve sortability).
dt2_format_datetime( options = list(), col_specs, from = NULL, to = "DD/MM/YYYY", locale = NULL, def = NULL )dt2_format_datetime( options = list(), col_specs, from = NULL, to = "DD/MM/YYYY", locale = NULL, def = NULL )
options |
List of options (returned with |
col_specs |
Column names or 1-based indices. |
from |
Input format (e.g., |
to |
Output format (e.g., |
locale |
Optional locale (e.g., |
def |
Optional default output if input is invalid. |
Modified options.
opts <- list(columns = c("when", "val")) opts <- dt2_format_datetime(opts, "when", from = "YYYY-MM-DD", to = "DD/MM/YYYY", locale = "pt-BR")opts <- list(columns = c("when", "val")) opts <- dt2_format_datetime(opts, "when", from = "YYYY-MM-DD", to = "DD/MM/YYYY", locale = "pt-BR")
Add a number renderer to one or more columns using
DataTables' built-in DataTable.render.number.
dt2_format_number( options = list(), col_specs, thousands = NULL, decimal = NULL, digits = 0, prefix = "", prefix_right = "" )dt2_format_number( options = list(), col_specs, thousands = NULL, decimal = NULL, digits = 0, prefix = "", prefix_right = "" )
options |
List of options (returned, with |
col_specs |
Column names or 1-based indices to format. |
thousands |
Thousands separator (character or |
decimal |
Decimal separator (character or |
digits |
Number of decimal places. |
prefix, prefix_right
|
String to prepend/append (e.g., currency symbol). |
Modified options.
opts <- list(columns = names(iris)) opts <- dt2_format_number(opts, "Sepal.Length", thousands = ".", decimal = ",", digits = 2, prefix = "", prefix_right = "")opts <- list(columns = names(iris)) opts <- dt2_format_number(opts, "Sepal.Length", thousands = ".", decimal = ",", digits = 2, prefix = "", prefix_right = "")
Adds a columns.render function that displays numbers as 1.2k, 3.4M, etc.
This renderer lets you control the number of decimal places via digits.
Use this when you want a fixed, compact style independent of locale rules.
dt2_format_number_abbrev( options = list(), col_specs, digits = 1, locale = NULL )dt2_format_number_abbrev( options = list(), col_specs, digits = 1, locale = NULL )
options |
A DataTables options list to be modified. |
col_specs |
Column names or 1-based indices to format. |
digits |
Integer, decimal places for the abbreviated display (default 1). |
locale |
Optional BCP-47 locale string (e.g. "pt-BR"). If provided,
the non-abbreviated part uses |
The modified options list.
opts <- list(columns = names(mtcars)) opts <- dt2_format_number_abbrev(opts, c("hp","qsec"), digits = 1, locale = "pt-BR")opts <- list(columns = names(mtcars)) opts <- dt2_format_number_abbrev(opts, c("hp","qsec"), digits = 1, locale = "pt-BR")
Format a date/time using DataTables' datetime renderer, with locale
dt2_format_time_format( options = list(), col_specs, from = NULL, to = "L", locale = "pt-br" )dt2_format_time_format( options = list(), col_specs, from = NULL, to = "L", locale = "pt-br" )
options |
Options list (returned modified). |
col_specs |
Column names or indices to format. |
from |
Input format (e.g. |
to |
Output format (e.g. |
locale |
Locale string, e.g. |
Modified options.
Relative time using moment.fromNow(), with locale
dt2_format_time_relative(options = list(), col_specs, locale = "pt-br")dt2_format_time_relative(options = list(), col_specs, locale = "pt-br")
options |
list of options (returned updated) |
col_specs |
names or indices to format |
locale |
e.g. "pt-br" (requires moment-with-locales) |
The modified options list with an updated columnDefs entry.
Language helper (either list or JSON url)
dt2_language(options = list(), lang_list = NULL, lang_url = NULL)dt2_language(options = list(), lang_list = NULL, lang_url = NULL)
options |
Options list. |
lang_list |
Named list of language keys. |
lang_url |
URL to a JSON translation file. |
Updated options.
order)Define initial ordering (option order)
dt2_order(options = list(), ...)dt2_order(options = list(), ...)
options |
Options list. |
... |
Vectors like |
Updated options.
Place a DT2 table in a Shiny UI.
dt2_output(outputId, width = "100%", height = "auto")dt2_output(outputId, width = "100%", height = "auto")
outputId |
Output ID (must match the |
width, height
|
CSS dimensions. |
An htmlwidgets Shiny output (HTML container) suitable for
inclusion in a Shiny UI definition.
Create a proxy for a DT2 table
dt2_proxy(id, session = shiny::getDefaultReactiveDomain())dt2_proxy(id, session = shiny::getDefaultReactiveDomain())
id |
Widget id used in |
session |
Shiny session. |
A "DT2Proxy" object.
Order the table (proxy)
dt2_proxy_order(proxy, ..., columns = NULL)dt2_proxy_order(proxy, ..., columns = NULL)
proxy |
|
... |
Vectors |
columns |
Optional character vector of column names to resolve names to indices. |
The proxy object, returned invisibly.
Page navigation (proxy)
dt2_proxy_page( proxy, page = c("first", "previous", "next", "last", "number"), number = NULL )dt2_proxy_page( proxy, page = c("first", "previous", "next", "last", "number"), number = NULL )
proxy |
A |
page |
Navigation action: |
number |
Page number (1-based). Only used when |
The proxy, invisibly.
Global search (proxy)
dt2_proxy_search( proxy, value, regex = FALSE, smart = TRUE, caseInsensitive = TRUE )dt2_proxy_search( proxy, value, regex = FALSE, smart = TRUE, caseInsensitive = TRUE )
proxy |
A |
value |
Search string. |
regex |
Logical; treat |
smart |
Logical; use DataTables smart search? Default: |
caseInsensitive |
Logical; case-insensitive search? Default: |
The proxy, invisibly.
Register a named JS renderer
dt2_register_renderer(name, js)dt2_register_renderer(name, js)
name |
Unique name (character scalar). |
js |
A |
Invisibly, the name.
Replace all data in the table (proxy)
dt2_replace_data(proxy, data)dt2_replace_data(proxy, data)
proxy |
|
data |
New data.frame (will be serialized). |
The proxy object, returned invisibly.
search)Set global search (option search)
dt2_search_global( options = list(), value, regex = FALSE, smart = TRUE, caseInsensitive = TRUE )dt2_search_global( options = list(), value, regex = FALSE, smart = TRUE, caseInsensitive = TRUE )
options |
Options list. |
value |
Text. |
regex, smart, caseInsensitive
|
Search flags. |
Updated options.
Select rows (proxy; Select extension)
dt2_select_rows(proxy, indexes, reset = TRUE)dt2_select_rows(proxy, indexes, reset = TRUE)
proxy |
|
indexes |
1-based row indices. |
reset |
If TRUE, clear selection before selecting. |
The proxy object, returned invisibly.
Default server-side handler (filter/order/page)
dt2_ssp_handler(names)dt2_ssp_handler(names)
names |
character() column names in display order. |
function(data, req) -> list(draw, recordsTotal, recordsFiltered, data)
Returns a list with reason, order, search, page, selected, state
reflecting the current client-side table state.
dt2_state(input, id)dt2_state(input, id)
input |
Shiny input object. |
id |
Widget ID. |
A list with the current table state.
Creates a theme object that can be passed to dt2() via the theme
parameter. Useful when you want the same look across many tables.
For quick one-off styling, you can also pass arguments directly to
dt2() (e.g., dt2(iris, striped = FALSE)).
dt2_theme( preset = "default", striped = NULL, hover = NULL, compact = NULL, font_scale = NULL, style = NULL, button_class = NULL )dt2_theme( preset = "default", striped = NULL, hover = NULL, compact = NULL, font_scale = NULL, style = NULL, button_class = NULL )
preset |
A named preset to start from: |
striped |
Logical; alternate row colours. |
hover |
Logical; highlight rows on hover. |
compact |
Logical; reduce cell padding. |
font_scale |
Numeric; font-size multiplier (e.g., 0.85 = 85%). |
style |
Styling framework: |
button_class |
CSS class string for Buttons extension buttons.
Default: |
A dt2_theme object (a named list).
# Create and reuse my_theme <- dt2_theme("clean", compact = TRUE) dt2(iris, theme = my_theme) dt2(mtcars, theme = my_theme) # Custom button style dt2_theme("default", button_class = "btn btn-sm btn-primary") # Presets dt2_theme("minimal") dt2_theme("compact")# Create and reuse my_theme <- dt2_theme("clean", compact = TRUE) dt2(iris, theme = my_theme) dt2(mtcars, theme = my_theme) # Custom button style dt2_theme("default", button_class = "btn btn-sm btn-primary") # Presets dt2_theme("minimal") dt2_theme("compact")
Checks for updates (respecting version constraints), patches version
numbers in the source files, and optionally runs
tools/get-dt2-libs.sh to download the new files.
dt2_update_libs(pkg_dir = ".", download = TRUE, dry_run = FALSE)dt2_update_libs(pkg_dir = ".", download = TRUE, dry_run = FALSE)
pkg_dir |
Path to the DT2 source root (the directory containing
|
download |
Logical. If |
dry_run |
Logical. If |
This function only works from the package source tree (i.e. during development). It will refuse to run from an installed package.
The workflow is:
Query npm for the latest compatible version of every library.
Patch tools/get-dt2-libs.sh (version variables).
Patch R/dt2_extensions.R (extension registry).
Patch R/dt2_check_updates.R (core lib versions).
Patch R/dt2_deps.R (DataTables core version).
Run bash tools/get-dt2-libs.sh to download the files.
Version constraints prevent incompatible upgrades:
jQuery is pinned to 3.x (DataTables 2 requires jQuery 3).
pdfmake is pinned to 0.2.x (0.3.x has breaking changes and is not available on cdnjs).
Bootstrap is pinned to 5.x.
Libraries marked as "PINNED" are skipped. Only "UPDATE"
items are applied.
Invisibly, a data.frame with the update results.
## Not run: # Developer-only tool: requires the DT2 package source tree # (DESCRIPTION, tools/get-dt2-libs.sh, R/dt2_extensions.R, ...). # It cannot run from an installed package, so it is not executable # in CRAN check or from a regular user session. # from the DT2 source root: dt2_update_libs() # preview changes without modifying anything: dt2_update_libs(dry_run = TRUE) ## End(Not run)## Not run: # Developer-only tool: requires the DT2 package source tree # (DESCRIPTION, tools/get-dt2-libs.sh, R/dt2_extensions.R, ...). # It cannot run from an installed package, so it is not executable # in CRAN check or from a regular user session. # from the DT2 source root: dt2_update_libs() # preview changes without modifying anything: dt2_update_libs(dry_run = TRUE) ## End(Not run)
Uses the modern DataTables 2.x layout API (not the deprecated dom).
dt2_use_buttons( options = list(), buttons = c("copy", "csv", "excel", "print"), position = "topEnd", button_class = NULL )dt2_use_buttons( options = list(), buttons = c("copy", "csv", "excel", "print"), position = "topEnd", button_class = NULL )
options |
Options list. |
buttons |
Vector of button ids (e.g., c("copy","csv","excel","print","colvis")). |
position |
Where to place buttons in the layout.
One of |
button_class |
CSS class for buttons (e.g., |
Updated options.
Use a named JS renderer on columns
dt2_use_renderer(options = list(), col_specs, name)dt2_use_renderer(options = list(), col_specs, name)
options |
Options list (returned modified). |
col_specs |
Column names or indices. |
name |
Name used in |
Modified options.
Listen for table events (init, draw, order, search, page, select, deselect).
observe_dt2_events(input, id, handler)observe_dt2_events(input, id, handler)
input |
Shiny input object. |
id |
Widget ID. |
handler |
Function with signature |
No return value, called for side effects. Sets up a Shiny
observer that calls handler whenever the table emits an event.
Render a DT2 table in a Shiny server function.
render_dt2(expr, env = parent.frame(), quoted = FALSE)render_dt2(expr, env = parent.frame(), quoted = FALSE)
expr |
Expression returning a |
env, quoted
|
Standard |
A Shiny render function (closure produced by
htmlwidgets::shinyRenderWidget()) that emits a DT2 widget.