DT: An R interface to the DataTables library (2024)

The R package DT provides an R interface to the JavaScript library DataTables. R data objects (matrices or data frames) can be displayed as tables on HTML pages, and DataTables provides filtering, pagination, sorting, and many other features in the tables.

You may install the stable version from CRAN, or the development version using remotes::install_github('rstudio/DT') if necessary (this website reflects the development version of DT):

# if (!require("DT")) install.packages('DT')xfun::session_info('DT')
## R version 4.3.0 (2023-04-21)## Platform: x86_64-apple-darwin20 (64-bit)## Running under: macOS Monterey 12.6.5## ## ## Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8## ## time zone: America/Chicago## tzcode source: internal## ## Package version:## base64enc_0.1.3 bslib_0.4.2 cachem_1.0.7 cli_3.6.1 ## crosstalk_1.2.0 digest_0.6.31 DT_0.27 ellipsis_0.3.2 ## evaluate_0.20.1 fastmap_1.1.1 fontawesome_0.5.1 fs_1.6.1 ## glue_1.6.2 graphics_4.3.0 grDevices_4.3.0 highr_0.10 ## htmltools_0.5.5 htmlwidgets_1.6.2 jquerylib_0.1.4 jsonlite_1.8.4 ## knitr_1.42.7 later_1.3.0 lazyeval_0.2.2 lifecycle_1.0.3 ## magrittr_2.0.3 memoise_2.0.1 methods_4.3.0 mime_0.12 ## promises_1.2.0.1 R6_2.5.1 rappdirs_0.3.3 Rcpp_1.0.10 ## rlang_1.1.0 rmarkdown_2.21 sass_0.4.5 stats_4.3.0 ## stringi_1.7.12 stringr_1.5.0 tinytex_0.45.1 tools_4.3.0 ## utils_4.3.0 vctrs_0.6.2 xfun_0.39 yaml_2.3.7

Please use Github issues to file bug reports or feature requests, and use StackOverflow to ask questions.

The main function in this package is datatable(). It creates an HTML widget to display R data objects with DataTables.

datatable(data, options = list(), class = "display", callback = JS("return table;"), rownames, colnames, container, caption = NULL, filter = c("none", "bottom", "top"), escape = TRUE, style = "auto", width = NULL, height = NULL, elementId = NULL, fillContainer = getOption("DT.fillContainer", NULL), autoHideNavigation = getOption("DT.autoHideNavigation", NULL), selection = c("multiple", "single", "none"), extensions = list(), plugins = NULL, editable = FALSE)

Here is a “hello world” example with zero configuration:

library(DT)datatable(iris)

If you are familiar with DataTables already, you may use the options argument to customize the table. See the page Options for details. Here we explain the rest of the arguments of the datatable() function.

2.1 Table CSS Classes

The class argument specifies the CSS classes of the table. The possible values can be found on the page of default styling options. The default value display basically enables row striping, row highlighting on mouse over, row borders, and highlighting ordered columns. You can choose a different combination of CSS classes, such as cell-border and stripe:

datatable(head(iris), class = 'cell-border stripe')

2.2 Styling

Currently, DT only supports the Bootstrap style besides the default style. You can use the argument style = 'bootstrap' to enable the Bootstrap style, and adjust the table classes accordingly using Bootstrap table class names, such as table-stripe and table-hover. Actually, DT will automatically adjust the class names even if you provided the DataTables class names such as stripe and hover.

DT:::DT2BSClass('display')## [1] "table table-striped table-hover row-border order-column display"DT:::DT2BSClass(c('compact', 'cell-border'))## [1] "table table-condensed table-bordered"

Note you can only use one style for all tables on one page. Please see this separate page for examples using the Bootstrap style.

2.3 Table Editing

You can enable table editing using the argument editable (see ?DT::datatable for its possible values). Then you will be able to double-click a cell to edit its value. It works in both client-side and server-side processing modes. Below are two client-side examples (also see a Shiny example with server-side processing):

DT::datatable(head(iris), editable = 'cell')
DT::datatable(head(iris), editable = list( target = 'row', disable = list(columns = c(1, 3, 4))))

2.4 Display Row Names

If the data object has row names, they will be displayed as the first column of the table by default. You can suppress row names via the argument rownames = FALSE, and you can also change row names by providing a different character vector to rownames.

datatable(head(mtcars))
datatable(head(mtcars), rownames = FALSE) # no row names
datatable(head(mtcars), rownames = head(LETTERS)) # new row names

Influence of Row Names on Column Indices in JavaScript

Row names are essentialy a new column added to the original data (via cbind(rownames(data), data)). This has an important consequence in terms of the column indices. JavaScript indexes from 0 instead of 1, so the index of the n-th element is actually n - 1.1 When thinking of the column indices (which you will often have to do if you customize options), use

  • n - 1 as the index of the n-th column in the original data if you do not display row names;
  • n as the index of the n-th column in the original data if you want to display row names, because the original index is n - 1 in JavaScript but we added the row names as the first column, and (n - 1) + 1 = n;

It is very important to remember this when using DataTables options.

2.5 Custom Column Names

By default, datatable() shows the column names of the data in the table, and you can use a custom character vector for the table header. There are a few possibilities. The first one is, you provide a new character vector to completely replace the column names of the data, e.g.

# colnames(iris) is a character vector of length 5, and we replace itdatatable(head(iris), colnames = c('Here', 'Are', 'Some', 'New', 'Names'))

This can be cumbersome if you only want to replace one or two names, and you do not want to provide a whole vector of names. Then here is the second possibility: you can provide a shorter numeric or character vector as the index vector to replace a subset of the column names. For example, if you only want the 2nd name to be 'A Nicer Name', you can use datatable(..., colnames = c('A Nicer Name' = 2)); or if you want to replace the name 'X5' with 'A Better Name', you can use colnames = c('A Better Name' = 'X5').

datatable(head(iris), colnames = c('A Better Name' = 'Sepal.Width'))
datatable(head(iris), colnames = c('Another Better Name' = 2, 'Yet Another Name' = 4))

When you display row names of the data, its column name will be a white space by default. That is why you cannot see its column name. You can certainly choose to use a column name for rownames as well, e.g.

# change the first column name to 'ID'datatable(head(iris), colnames = c('ID' = 1))

2.6 Custom Table Container

The container argument allows you to provide a different table container to hold the table cells. By default, the container is generated from the column names. Below is an example of a custom table header:

# a custom table containersketch = htmltools::withTags(table( class = 'display', thead( tr( th(rowspan = 2, 'Species'), th(colspan = 2, 'Sepal'), th(colspan = 2, 'Petal') ), tr( lapply(rep(c('Length', 'Width'), 2), th) ) )))print(sketch)
<table class="display"> <thead> <tr> <th rowspan="2">Species</th> <th colspan="2">Sepal</th> <th colspan="2">Petal</th> </tr> <tr> <th>Length</th> <th>Width</th> <th>Length</th> <th>Width</th> </tr> </thead></table>
# use rownames = FALSE here because we did not generate a cell for row names in# the header, and the header only contains five columnsdatatable(iris[1:20, c(5, 1:4)], container = sketch, rownames = FALSE)

You can also add a footer to the table container, and here is an example:

# a custom table with both header and footersketch = htmltools::withTags(table( tableHeader(iris), tableFooter(iris)))print(sketch)
<table> <thead> <tr> <th>Sepal.Length</th> <th>Sepal.Width</th> <th>Petal.Length</th> <th>Petal.Width</th> <th>Species</th> </tr> </thead> <tfoot> <tr> <th>Sepal.Length</th> <th>Sepal.Width</th> <th>Petal.Length</th> <th>Petal.Width</th> <th>Species</th> </tr> </tfoot></table>
datatable( head(iris, 10), container = sketch, options = list(pageLength = 5, dom = 'tip'), rownames = FALSE)

2.7 Table Caption

You can add a table caption via the caption argument. It can be either a character vector, or a tag object created from htmltools::tags$caption(). See this blog post for more information on table captions.

datatable( head(iris), caption = 'Table 1: This is a simple caption for the table.')
# display the caption at the bottom, and <em> the captiondatatable( head(iris), caption = htmltools::tags$caption( style = 'caption-side: bottom; text-align: center;', 'Table 2: ', htmltools::em('This is a simple caption for the table.') ))

2.8 Column Filters

DataTables does not provide column filters by default. There is only a global filter (the search box on the top-right). We added a filter argument in datatable() to automatically generate column filters. By default, the filters are not shown since filter = 'none'. You can enable these filters by filter = 'top' or 'bottom', depending on whether you want to put the filters on the top or bottom of the table.

iris2 = iris[c(1:10, 51:60, 101:110), ]datatable(iris2, filter = 'top', options = list( pageLength = 5, autoWidth = TRUE))

\n

\n

\n \n \n

\n \n

\n

\n \n \n

\n

\n

\n \n \n

\n

\n

\n

\n \n \n

\n

\n

\n \n \n

\n

\n

\n

\n \n \n

\n

\n

\n \n \n

\n

\n

\n

\n \n \n

\n

\n \n

\n

\n", "data": [ ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110"], [5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 5, 4.4, 4.9, 7, 6.4, 6.9, 5.5, 6.5, 5.7, 6.3, 4.9, 6.6, 5.2, 6.3, 5.8, 7.1, 6.3, 6.5, 7.6, 4.9, 7.3, 6.7, 7.2], [3.5, 3, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.2, 3.2, 3.1, 2.3, 2.8, 2.8, 3.3, 2.4, 2.9, 2.7, 3.3, 2.7, 3, 2.9, 3, 3, 2.5, 2.9, 2.5, 3.6], [1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 4.7, 4.5, 4.9, 4, 4.6, 4.5, 4.7, 3.3, 4.6, 3.9, 6, 5.1, 5.9, 5.6, 5.8, 6.6, 4.5, 6.3, 5.8, 6.1], [0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 1.4, 1.5, 1.5, 1.3, 1.5, 1.3, 1.6, 1, 1.3, 1.4, 2.5, 1.9, 2.1, 1.8, 2.2, 2.1, 1.7, 1.8, 1.8, 2.5], ["setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "setosa", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "versicolor", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica", "virginica"] ], "container": "

\n \n \n \n \n \n \n \n \n \n \n
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies

", "options": { "pageLength": 5, "autoWidth": true, "columnDefs": [ { "className": "dt-right", "targets": [1, 2, 3, 4] }, { "orderable": false, "targets": 0 } ], "order": [], "orderClasses": false, "orderCellsTop": true, "lengthMenu": [5, 10, 25, 50, 100] } }, "evals": [], "jsHooks": []}

Depending on the type of a column, the filter control can be different. Initially, you see search boxes for all columns. When you click the search boxes, you may see different controls:

  • For numeric/date/time columns, range sliders are used to filter rows within ranges;
  • For factor columns, selectize inputs are used to display all possible categories, and you can select multiple categories there (note you can also type in the box to search in all categories);
  • For character columns, ordinary search boxes are used to match the values you typed in the boxes;

When you leave the initial search boxes, the controls will be hidden and the filtering values (if there are any) are stored in the boxes:

  • For numeric/date/time columns, the values displayed in the boxes are of the form low ... high;
  • For factor columns, the values are serialized as a JSON array of the form ["value1", "value2", "value3"];

When a column is filtered, there will be a clear button in its search box, and you can click the button to clear the filter. If you do not want to use the controls, you can actually type in the search boxes directly, e.g.you may type 2 ... 5 to filter a numeric column, and the range of its slider will automatically adjusted to [2, 5]. In case you find a search box too narrow and it is difficult to read the values in it, you may mouse over the box and its values will be displayed as a tooltip. See this example for how to hide the clear buttons, and use plain text input styles instead of Bootstrap.

Below is a simple example to demonstrate filters for character, date, and time columns:

d = data.frame( names = rownames(mtcars), date = as.Date('2015-03-23') + 1:32, time = as.POSIXct('2015-03-23 12:00:00', tz = 'UTC') + (1:32) * 5000, stringsAsFactors = FALSE)str(d)## 'data.frame': 32 obs. of 3 variables:## $ names: chr "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...## $ date : Date, format: "2015-03-24" "2015-03-25" ...## $ time : POSIXct, format: "2015-03-23 13:23:20" "2015-03-23 14:46:40" ...datatable(d, filter = 'bottom', options = list(pageLength = 5))

\n \n

\n

\n \n \n

\n

\n

\n \n \n

\n

\n

\n

\n \n \n

\n

\n

\n \n \n

\n

\n", "data": [ ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32"], ["Mazda RX4", "Mazda RX4 Wag", "Datsun 710", "Hornet 4 Drive", "Hornet Sportabout", "Valiant", "Duster 360", "Merc 240D", "Merc 230", "Merc 280", "Merc 280C", "Merc 450SE", "Merc 450SL", "Merc 450SLC", "Cadillac Fleetwood", "Lincoln Continental", "Chrysler Imperial", "Fiat 128", "Honda Civic", "Toyota Corolla", "Toyota Corona", "Dodge Challenger", "AMC Javelin", "Camaro Z28", "Pontiac Firebird", "Fiat X1-9", "Porsche 914-2", "Lotus Europa", "Ford Pantera L", "Ferrari Dino", "Maserati Bora", "Volvo 142E"], ["2015-03-24", "2015-03-25", "2015-03-26", "2015-03-27", "2015-03-28", "2015-03-29", "2015-03-30", "2015-03-31", "2015-04-01", "2015-04-02", "2015-04-03", "2015-04-04", "2015-04-05", "2015-04-06", "2015-04-07", "2015-04-08", "2015-04-09", "2015-04-10", "2015-04-11", "2015-04-12", "2015-04-13", "2015-04-14", "2015-04-15", "2015-04-16", "2015-04-17", "2015-04-18", "2015-04-19", "2015-04-20", "2015-04-21", "2015-04-22", "2015-04-23", "2015-04-24"], ["2015-03-23T13:23:20Z", "2015-03-23T14:46:40Z", "2015-03-23T16:10:00Z", "2015-03-23T17:33:20Z", "2015-03-23T18:56:40Z", "2015-03-23T20:20:00Z", "2015-03-23T21:43:20Z", "2015-03-23T23:06:40Z", "2015-03-24T00:30:00Z", "2015-03-24T01:53:20Z", "2015-03-24T03:16:40Z", "2015-03-24T04:40:00Z", "2015-03-24T06:03:20Z", "2015-03-24T07:26:40Z", "2015-03-24T08:50:00Z", "2015-03-24T10:13:20Z", "2015-03-24T11:36:40Z", "2015-03-24T13:00:00Z", "2015-03-24T14:23:20Z", "2015-03-24T15:46:40Z", "2015-03-24T17:10:00Z", "2015-03-24T18:33:20Z", "2015-03-24T19:56:40Z", "2015-03-24T21:20:00Z", "2015-03-24T22:43:20Z", "2015-03-25T00:06:40Z", "2015-03-25T01:30:00Z", "2015-03-25T02:53:20Z", "2015-03-25T04:16:40Z", "2015-03-25T05:40:00Z", "2015-03-25T07:03:20Z", "2015-03-25T08:26:40Z"] ], "container": "

\n \n \n \n \n \n \n \n \n
namesdatetime

", "options": { "pageLength": 5, "columnDefs": [ { "orderable": false, "targets": 0 } ], "order": [], "autoWidth": false, "orderClasses": false, "lengthMenu": [5, 10, 25, 50, 100] } }, "evals": [], "jsHooks": []}

Filtering in the above examples was done on the client side (using JavaScript in your web browser). Column filters also work in the server-side processing mode, in which case filtering will be processed on the server, and there may be some subtle differences (e.g.JavaScript regular expressions are different with R). See here for an example of column filters working on the server side.

Known Issues of Column Filters

The position of column filters may be off when scrolling is enabled in the table, e.g.via the options scrollX and/or scrollY. The appearance may be affected by Shiny sliders, as reported in #49.

DT: An R interface to the DataTables library (2024)
Top Articles
Latest Posts
Article information

Author: Madonna Wisozk

Last Updated:

Views: 5905

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.