
Translating DataTables.net Config to R
DT2 Team
2026-04-29
Source:vignettes/js-config.Rmd
js-config.RmdThe 1:1 Mapping Principle
DT2’s options argument maps directly to the DataTables
JavaScript configuration object. An R named list becomes a JS object; R
vectors become JS arrays. This means you can translate any example from
datatables.net directly
to R.
Translation Rules
| JavaScript | R |
|---|---|
{ key: value } |
list(key = value) |
[1, 2, 3] |
c(1, 2, 3) or list(1, 2, 3)
|
true / false
|
TRUE / FALSE
|
null |
NULL |
"string" |
"string" |
function(d) { ... } |
htmlwidgets::JS("function(d) { ... }") |
Layout — The Complete Guide
DataTables 2 replaced the old dom string with
layout, a structured way to position elements around the
table. This is the most important change from DataTables 1.x.
Position grid
+------------------+------------------+
| topStart | topEnd |
+------------------+------------------+
| top (full width) |
+------------------+------------------+
| top2Start | top2End |
+------------------+------------------+
| TABLE |
+------------------+------------------+
| bottomStart | bottomEnd |
+------------------+------------------+
| bottom (full width) |
+------------------+------------------+
| bottom2Start | bottom2End |
+------------------+------------------+
Available elements
| Element | Description |
|---|---|
"search" |
Search/filter input |
"paging" |
Page navigation |
"info" |
“Showing X to Y of Z entries” |
"pageLength" |
Entries per page selector |
"buttons" |
Buttons toolbar (requires Buttons extension) |
"searchBuilder" |
SearchBuilder (requires extension) |
"searchPanes" |
SearchPanes (requires extension) |
NULL |
Remove whatever would normally be in that position |
Default layout
When you don’t specify layout, DataTables uses:
layout = list(
topStart = "pageLength",
topEnd = "search",
bottomStart = "info",
bottomEnd = "paging"
)Buttons in layout
Place the Buttons toolbar in any position:
Complete custom layout
dt2(mtcars[1:20, ], options = list(
pageLength = 10,
buttons = list(
list(extend = "collection", text = "Export \u25BC",
buttons = list("copyHtml5", "csvHtml5", "excelHtml5")),
list(extend = "colvis", text = "Columns")
),
layout = list(
topStart = "buttons",
topEnd = list(search = list(placeholder = "Filter...")),
bottomStart = "info",
bottomEnd = "paging"
)
))Migrating from dom
If you’re coming from DataTables 1.x or the DT package:
Old dom
|
New layout
|
|---|---|
"frtip" |
(default — no need to specify) |
"tp" |
list(topStart = NULL, topEnd = NULL, bottomStart = NULL, bottomEnd = "paging") |
"Bfrtip" |
list(topStart = "buttons") |
"lfBrtip" |
list(topStart = list("pageLength", "buttons")) |
DT2 will automatically convert dom strings containing
"B" to the layout equivalent, but using
layout directly is recommended.
Using htmlwidgets::JS() for Callbacks
Whenever DataTables expects a JavaScript function, wrap it in
htmlwidgets::JS():
Column Renderers
Built-in DataTables Renderers
DataTables v2 provides built-in renderers accessible via
DataTable.render.*:
Custom Render Function
progress_render <- htmlwidgets::JS("
function(data, type, row, meta) {
if (type !== 'display') return data;
var pct = Math.min(100, Math.max(0, parseFloat(data)));
var color = pct > 70 ? '#198754' : (pct > 40 ? '#ffc107' : '#dc3545');
return '<div style=\"background:#eee;border-radius:4px;overflow:hidden\">' +
'<div style=\"width:' + pct + '%;background:' + color +
';height:14px;border-radius:4px\"></div></div>';
}
")
df <- data.frame(
task = c("Design", "Backend", "Testing", "Deploy"),
progress = c(85, 60, 30, 95)
)
dt2(df, options = list(
pageLength = 10,
columnDefs = list(
list(targets = 1, render = progress_render)
)
))Internationalization (i18n)
Inline language object
dt2(iris[1:10, ], options = list(
pageLength = 5,
language = list(
search = "Buscar:",
lengthMenu = "Mostrar _MENU_ registros",
info = "Mostrando _START_ a _END_ de _TOTAL_",
paginate = list(
first = "<<",
previous = "<",
`next` = ">",
last = ">>"
),
zeroRecords = "Nenhum registro encontrado",
emptyTable = "Tabela vazia"
)
))