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 (#41);
  • These extensions do/may not work in Shiny apps:
  • 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

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

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

11 TableTools

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