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);
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.
You can click and drag the table header to move a certain column to a different place using the
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.
m = as.data.frame(round(matrix(rnorm(100), 5), 5)) datatable( m, extensions = 'FixedColumns', options = list( dom = 't', scrollX = TRUE, fixedColumns = TRUE ) )
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.
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).
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.
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.
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( iris2, colnames = c(ID = 1), extensions = 'RowReorder', 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.
m = matrix(runif(1000 * 4), ncol = 4, dimnames = list(NULL, letters[1:4])) m = cbind(id = seq_len(nrow(m)), round(m, 2)) 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
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.
TableTools extension is retired and has been removed from DT. Please use the
Buttons extension instead.