DataTables has a number of extensions, and all of them have been integrated into DT. You can use the
extensions argument of
datatable() to add extensions to the table. Although we only use one extension in each section below, it is possible to apply multiple extensions to one table by providing the names of the extensions as a character vector, e.g.
extensions = c('AutoFill', 'ColReorder'). Please take a look at the feature compatibility chart to know which extensions can be used together.
Most DataTables extensions work on their own, but some may not work well when certain non-default features are enabled or used in Shiny apps. Below is a list of known issues:
Scroller(#34, #41, #422);
FixedColumnsextension does not work well with row selection: when you select rows in the fixed columns, the rows in other columns won’t be selected, and vice (#275, #395, #439);
FixedHeaderextension does not work in tabsets (#389);
# this is the example data we use on this page = head(iris, 30)iris2
AutoFill extension, you will see a blue square in the bottom-right corner of a cell when you mouse over the cell. You can drag it to automatically fill the column.
datatable(iris2, extensions = 'AutoFill', options = list(autoFill = TRUE))
# pass some initialization options to AutoFill: only enable auto filling on the # columns 2, 3, 4; show the blue square on click instead of hover datatable(iris2, extensions = 'AutoFill', options = list( autoFill = list(columns = c(1, 2, 3), focus = 'click') ))
You can click and drag the table header to move a certain column to a different place using the
datatable(iris2, extensions = 'ColReorder', options = list(colReorder = TRUE))
# disable realtime updating datatable(iris2, extensions = 'ColReorder', options = list( colReorder = list(realtime = FALSE) ))
# exclude the first two columns (i.e. they are always visible) datatable( rownames = FALSE, iris2, extensions = 'Buttons', options = list( dom = 'Bfrtip', buttons = list(list(extend = 'colvis', columns = c(2, 3, 4))) ))
When a table is too wide, you can fix certain columns (e.g. the left and/or the right column) using the
FixedColumns extension so that they will be visible when you scroll the table horizontally.
= as.data.frame(round(matrix(rnorm(100), 5), 5)) m datatable( extensions = 'FixedColumns', m, options = list( dom = 't', scrollX = TRUE, fixedColumns = TRUE ))
# fix some left 2 columns and right 1 column datatable( extensions = 'FixedColumns', m, options = list( dom = 't', scrollX = TRUE, fixedColumns = list(leftColumns = 2, rightColumns = 1) ))
You may want the table header to be always visible when scrolling down the table, and you can use the
FixedHeader extension in this case.
datatable( extensions = 'FixedHeader', iris, options = list(pageLength = 50, fixedHeader = TRUE) )
Since we have a navigation bar on the top of this page, this extension does not work well on this particular page (the table header will be hidden behind the navigation bar). Please see this separate page for a real example.
KeyTable extension provides the ability of navigating through the table like Excel: a cell is highlighted by a box, and you can move around using the arrow keys (Left/Right/Up/Down).
datatable(iris2, extensions = 'KeyTable', options = list(keys = TRUE))
Responsive extension makes the table columns responsive in the sense that when the page is too narrow, certain columns can be automatically collapsed and hidden. In the example below, as you decrease the page width, you will see a sequence of columns collapsed and a button appear on the left. You can click the button to expand the data of the hidden columns behind it.
datatable(iris2, extensions = 'Responsive')
RowGroup extension provides an easy way to use the row grouping feature. In the example below,
cyl (number of cylinders) is used as the row group column and its value is displayed as the row group label.
= mtcars[1:20, ] mtcars2 datatable( order(mtcars2$cyl), ], mtcars2[extensions = 'RowGroup', options = list(rowGroup = list(dataSrc = 2)), selection = 'none' )
RowReorder extension allows you to drag and reorder a certain row in the table. Note the first column
ID is not changed when you reorder the rows in the example below, which can be a little confusing:
datatable( colnames = c(ID = 1), extensions = 'RowReorder', iris2, options = list(rowReorder = TRUE, order = list(c(0 , 'asc'))) )
In order to make
RowReorder work, there must be at least one sorted column in the table, which can be initialized by
options = list(order = list(c(0 , 'asc'))).
When the table has a large number of rows, you may not want to render all rows on the page immediately since it will be slow. The
Scroller extension makes it possible to only render the visible portion of the table.
= matrix(runif(1000 * 4), ncol = 4, dimnames = list(NULL, letters[1:4])) m = cbind(id = seq_len(nrow(m)), round(m, 2)) m datatable(m, extensions = 'Scroller', options = list( deferRender = TRUE, scrollY = 200, scroller = TRUE ))
You can also save the data as JSON in a file and load it via the
ajax option. See a live example in Shiny here.
SearchPanes extension is a new extension that adds panes to the DataTable with the capability to search the DataTable by selecting rows in the panes. As documented in the realsing blog, it doesn’t support the server-side processing mode. Since it depends on the
Select extension, you are expected to always include the
Select extension with
SearchPanes. In addition, in order to display the search panes, you need to insert
P to the
datatable( iris,options = list(dom = 'Pfrtip', columnDefs = list(list( searchPanes = list(show = FALSE), targets = 1:4 ))),extensions = c('Select', 'SearchPanes'), selection = 'none' )
Select extension allows the user to perform item selections in DataTable. It supports three different selection styles including
single(Single item select) and
multiple(Multi-item selection) and has built-in button types for the
Buttons extension allowing select all / none and other actions to be performed very easily. However, it only works well in the client-side processing mode, i.e.,
DT::renderDT(..., server = FALSE). Note that you need to turn off DT’s own select functionality by setting
selection = 'none' when you want to use the
Select extension in shiny.
DT also has its own implementation of row/column/cell selections in Shiny (see the
selection param in
?DT::datatable), which is different from the
Select extension. In particular, it only supports Shift/Ctrl + Click for selecting multiple rows at the time of writing (see #305 for an example). On the other hand, DT’s implementation supports both client and server-side processing mode.
datatable( extensions = c('Select', 'Buttons'), options = list( iris, select = list(style = 'os', items = 'row'), dom = 'Blfrtip', rowId = 0, buttons = c('selectAll', 'selectNone', 'selectRows', 'selectColumns', 'selectCells') ),selection = 'none' )
TableTools extension is retired and has been removed from DT. Please use the
Buttons extension instead.