Last data update: 2014.03.03

R: Create an HTML table widget using the DataTables library
datatableR Documentation

Create an HTML table widget using the DataTables library

Description

This function creates an HTML widget to display rectangular data (a matrix or data frame) using the JavaScript library DataTables.

Usage

datatable(data, options = list(), class = "display", callback = JS("return table;"), 
    rownames, colnames, container, caption = NULL, filter = c("none", "bottom", "top"), 
    escape = TRUE, style = "default", selection = c("multiple", "single", "none"), 
    extensions = list())

Arguments

data

a data object (either a matrix or a data frame)

options

a list of initialization options (see http://datatables.net/reference/option/); the character options wrapped in JS() will be treated as literal JavaScript code instead of normal character strings; you can also set options globally via options(DT.options = list(...)), and global options will be merged into this options argument if set

class

the CSS class(es) of the table; see http://datatables.net/manual/styling/classes

callback

the body of a JavaScript callback function with the argument table to be applied to the DataTables instance (i.e. table)

rownames

TRUE (show row names) or FALSE (hide row names) or a character vector of row names; by default, the row names are displayed in the first column of the table if exist (not NULL)

colnames

if missing, the column names of the data; otherwise it can be an unnamed character vector of names you want to show in the table header instead of the default data column names; alternatively, you can provide a named numeric or character vector of the form 'newName1' = i1, 'newName2' = i2 or c('newName1' = 'oldName1', 'newName2' = 'oldName2', ...), where newName is the new name you want to show in the table, and i or oldName is the index of the current column name

container

a sketch of the HTML table to be filled with data cells; by default, it is generated from htmltools::tags$table() with a table header consisting of the column names of the data

caption

the table caption; a character vector or a tag object generated from htmltools::tags$caption()

filter

whether/where to use column filters; none: no filters; bottom/top: put column filters at the bottom/top of the table; range sliders are used to filter numeric/date/time columns, select lists are used for factor columns, and text input boxes are used for character columns; if you want more control over the styles of filters, you can provide a list to this argument of the form list(position = 'top', clear = TRUE, plain = FALSE), where clear indicates whether you want the clear buttons in the input boxes, and plain means if you want to use Bootstrap form styles or plain text input styles for the text input boxes

escape

whether to escape HTML entities in the table: TRUE means to escape the whole table, and FALSE means not to escape it; alternatively, you can specify numeric column indices or column names to indicate which columns to escape, e.g. 1:5 (the first 5 columns), c(1, 3, 4), or c(-1, -3) (all columns except the first and third), or c('Species', 'Sepal.Length')

style

the style name (http://datatables.net/manual/styling/); currently only 'default' and 'bootstrap' are supported

selection

the row selection mode (single or multiple selection or disable selection) when a table widget is rendered in a Shiny app

extensions

a character vector of the names of the DataTables extensions (http://datatables.net/extensions/index), or a named list of initialization options for the extensions (the names of the list are the names of extensions)

Note

You are recommended to escape the table content for security reasons (e.g. XSS attacks) when using this function in Shiny or any other dynamic web applications.

References

See http://rstudio.github.io/DT for the full documentation.

Examples

library(DT)

# see the package vignette for examples and the link to website
vignette('DT', package = 'DT')

# some boring edge cases for testing purposes
m = matrix(nrow = 0, ncol = 5, dimnames = list(NULL, letters[1:5]))
datatable(m)  # zero rows
datatable(as.data.frame(m))

m = matrix(1, dimnames = list(NULL, 'a'))
datatable(m)  # one row and one column
datatable(as.data.frame(m))

m = data.frame(a = 1, b = 2, c = 3)
datatable(m)
datatable(as.matrix(m))

# dates
datatable(data.frame(
  date = seq(as.Date("2015-01-01"), by = "day", length.out = 5), x = 1:5
))
datatable(data.frame(x = Sys.Date()))
datatable(data.frame(x = Sys.time()))

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(DT)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/DT/datatable.Rd_%03d_medium.png", width=480, height=480)
> ### Name: datatable
> ### Title: Create an HTML table widget using the DataTables library
> ### Aliases: datatable
> 
> ### ** Examples
> 
> library(DT)
> 
> # see the package vignette for examples and the link to website
> vignette('DT', package = 'DT')
starting httpd help server ... done
> 
> # some boring edge cases for testing purposes
> m = matrix(nrow = 0, ncol = 5, dimnames = list(NULL, letters[1:5]))
> datatable(m)  # zero rows
> datatable(as.data.frame(m))
> 
> m = matrix(1, dimnames = list(NULL, 'a'))
> datatable(m)  # one row and one column
> datatable(as.data.frame(m))
> 
> m = data.frame(a = 1, b = 2, c = 3)
> datatable(m)
> datatable(as.matrix(m))
> 
> # dates
> datatable(data.frame(
+   date = seq(as.Date("2015-01-01"), by = "day", length.out = 5), x = 1:5
+ ))
> datatable(data.frame(x = Sys.Date()))
> datatable(data.frame(x = Sys.time()))
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>