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.
{DT}
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
📘 documentation → github
⭐️ more than 500 stars on github
To get started with DT
, you can install it directly from
CRAN using the install.packages
function:
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")
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")
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")
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")
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")