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.

Known Issues of Extensions

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:

  • Column filters may not work well with the extensions FixedColumns, FixedHeader, and Scroller (#34, #41, #422);
  • The FixedColumns extension 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);
  • The FixedHeader extension does not work in tabsets (#389);
  • These extensions do/may not work in Shiny apps:
    • ColReorder will fail when you use formatting functions (#250).
    • KeyTable (#132)
    • Responsive (#69)
    • Scroller does not work with column selection (#283).
  • The Excel button in the Buttons extension may not appear in Safari (#343);
# this is the example data we use on this page
iris2 = head(iris, 30)

1 AutoFill

With the 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')
))

2 Buttons

The Buttons extension adds some buttons to the table to copy the table to clipboard, save the table as CSV/XLS/PDF, and print the table, etc. Here are some built-in buttons provided by this extension (note B in dom denotes the place where the buttons are to be inserted):

datatable(
  iris, extensions = 'Buttons', options = list(
    dom = 'Bfrtip',
    buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
  )
)

Below are more examples of Buttons:

3 ColReorder

You can click and drag the table header to move a certain column to a different place using the ColReorder extension.

datatable(iris2, extensions = 'ColReorder', options = list(colReorder = TRUE))
# disable realtime updating
datatable(iris2, extensions = 'ColReorder', options = list(
  colReorder = list(realtime = FALSE)
))

4 ColVis (Retired)

The ColVis extension has been removed from DT after v0.1 because it was replaced by the Buttons extension in the DataTables library. You can use the button colvis instead.

# using I() here to make sure it is converted to an array in JavaScript
datatable(
  iris2, rownames = FALSE,
  extensions = 'Buttons', options = list(dom = 'Bfrtip', buttons = I('colvis'))
)
# exclude the first two columns (i.e. they are always visible)
datatable(
  iris2, rownames = FALSE,
  extensions = 'Buttons', options = list(
    dom = 'Bfrtip',
    buttons = list(list(extend = 'colvis', columns = c(2, 3, 4)))
  )
)

5 FixedColumns

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
  )
)
# fix some left 2 columns and right 1 column
datatable(
  m, extensions = 'FixedColumns',
  options = list(
    dom = 't',
    scrollX = TRUE,
    fixedColumns = list(leftColumns = 2, rightColumns = 1)
  )
)

6 FixedHeader

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(
  iris, extensions = 'FixedHeader',
  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.

7 KeyTable

The 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))

8 Responsive

The 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')

9 RowGroup

The 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.

mtcars2 = mtcars[1:20, ]
datatable(
  mtcars2[order(mtcars2$cyl), ],
  extensions = 'RowGroup',
  options = list(rowGroup = list(dataSrc = 2)),
  selection = 'none'
)

10 RowReorder

The 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'))).

11 Scroller

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.

12 SearchPanes

The 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 dom option.

datatable(
  iris,
  options = list(dom = 'Pfrtip', columnDefs = list(list(
    searchPanes = list(show = FALSE), targets = 1:4
  ))),
  extensions = c('Select', 'SearchPanes'),
  selection = 'none'
)

13 Select

The Select extension allows the user to perform item selections in DataTable. It supports three different selection styles including os(Operating system), 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(
  iris, extensions = c('Select', 'Buttons'), options = list(
    select = list(style = 'os', items = 'row'),
    dom = 'Blfrtip',
    rowId = 0,
    buttons = c('selectAll', 'selectNone', 'selectRows', 'selectColumns', 'selectCells')
  ),
  selection = 'none'
)

14 TableTools (Retired)

The TableTools extension is retired and has been removed from DT. Please use the Buttons extension instead.