Create interactive table with DT


This post explains how display R data object (such as dataframe) as HTML pages with filtering, editing and even use callback javascript functions with the DT package. It provides several reproducible examples with explanation and R code.
We showcase the key features of DT and provides a set of table examples using the package.

Documentation

{DT}

Quick start


The DT package is an interface for the DataTable JavaScript library in R. It can be used for rendering interactive and dynamic tables in R Markdown documents and Shiny web applications.

It (mainly) offers a datatable() function that creates an HTML widget that display a table. This table is interactive and dynamic thanks to the use of JavaScript.

✍️ author → Yihui Xie

📘 documentationgithub

⭐️ more than 500 stars on github

Installation


To get started with DT, you can install it directly from CRAN using the install.packages function:

install.packages("DT")

Basic usage


Using DT is straightforward. It provides a datatable() function. You just have to provide a dataframe to this function and that’s it, you get the interactive table!

Here’s a basic example:

library(DT)
data(mtcars)

table = datatable(mtcars)
table

# save the widget
# library(htmlwidgets)
# saveWidget(table, file="../HtmlWidget/dt-table-basic.html")

Key features



→ Use CSS classes

In the datatable() function, there is a cell argument that can take CSS classes. The available ones are: display, cell-border, compact hover, nowrap, order-column, row-border, stripe.

Example using cell-border, stripe, hover and compact:

library(DT)

table = datatable(
  mtcars, 
  class = 'cell-border stripe hover compact'
)
table

# save the widget
# library(htmlwidgets)
# saveWidget(table, file="../HtmlWidget/dt-table-1.html")


→ Add caption

Thanks to the caption argument and the htmltools library, you can add customized caption to the table.

Example:

library(DT)
library(htmltools)
data(mtcars)

table <- datatable(
  mtcars,
  caption = tags$caption(
    style = 'caption-side: bottom; text-align: center;',
    'Table 1: ', em('The mtcars dataset is a dataset about cars properties')
  )
)
table 

# save widget
# library(htmltools)
# saveWidget(table, file="../HtmlWidget/dt-table-2.html")


→ Filtering

The datatable() function has a filtering option. Depending on the type of variable, the filtering might differ: - quantitative columns: range slider - qualitative columns: select from all possible categories

Example:

library(DT)
data(mtcars)

table <- datatable(mtcars,
  filter = "top", # put filters on top of the table
)
table

# save widget
# library(htmltools)
# saveWidget(table, file = "../HtmlWidget/dt-table-3.html")


→ Callback JavaScript functions

You can apply JavaScript functions to your tables with the callback argument. The following example starts the table on the second page.

Example:

library(DT)
data(mtcars)

table <- datatable(mtcars,
          callback = JS('table.page("next").draw(false);'))
table

# save widget
# library(htmltools)
# saveWidget(table, file="../HtmlWidget/dt-table-4.html")


→ Custom style with colors, urls…

For this example, we’ll create a specific dataset for better understanding.

Example:

library(DT)

# create dataset
data = matrix(c(
  '<b>Bold</b>', '<em>Emphasize</em>', '<a href="https://r-graph-gallery.com/package/dt.html">Click here</a>',
  '<a href="#" onclick="alert(\'This message is displayed thanks to DT table!\');">Click there</a>'),
  2)

#change columns name
colnames(data) = c('<span style="color:red">Red column</span>', '<em>Italic column</em>')

table <- datatable(data,
          escape = FALSE # this argument allows the content of the cells to be interpreted as HTML
          )

# save widget
# library(htmltools)
# saveWidget(table, file="../HtmlWidget/dt-table-5.html")



Contact

This document is a work by Yan Holtz. Any feedback is highly encouraged. You can fill an issue on Github, drop me a message on Twitter, or send an email pasting yan.holtz.data with gmail.com.

Github Twitter