DT Package

The R package DT provides an R interface to the JavaScript library DataTables.

library(DT)
datatable(iris)

CSS classes of the table

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

Enable table editing

double-click a cell to edit its value.

datatable(head(iris), editable = 'cell')

Change the column name

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

Table caption

datatable(
  head(iris),
  caption = 'Table 1: This is a simple caption for the table.'
)

Column Filters

datatable(iris, filter = 'top', options = list(
  pageLength = 5, autoWidth = TRUE
))
datatable(head(iris, 30), callback = JS('table.page("next").draw(false);'))

Formatting Functions

df <- data.frame(
  A = rpois(100, 5),
  B = runif(100),
  C = rpois(100,5),
  D = rnorm(100),
  E = Sys.Date() + 1:100
)
datatable(df) %>%
  formatCurrency(c('A', 'C'), '$') %>%
  formatPercentage('B', 2) %>%
  formatRound('D', 3) %>%
  formatDate('E', 'toDateString')

Style Table Cells

datatable(iris, options = list(pageLength = 5)) %>%
  formatStyle('Sepal.Length',  color = 'red', backgroundColor = 'orange', fontWeight = 'bold')
datatable(iris) %>% 
  formatStyle('Sepal.Length', fontWeight = styleInterval(5, c('normal', 'bold'))) %>%
  formatStyle(
    'Sepal.Width',
    color = styleInterval(c(3.4, 3.8), c('white', 'blue', 'red')),
    backgroundColor = styleInterval(3.4, c('gray', 'yellow'))
  ) %>%
  formatStyle(
    'Petal.Length',
    background = styleColorBar(iris$Petal.Length, 'steelblue'),
    backgroundSize = '100% 90%',
    backgroundRepeat = 'no-repeat',
    backgroundPosition = 'center'
  ) %>%
  formatStyle(
    'Species',
    transform = 'rotateX(45deg) rotateY(20deg) rotateZ(30deg)',
    backgroundColor = styleEqual(
      unique(iris$Species), c('lightblue', 'lightgreen', 'lightpink')
    )
  )