The R package DT provides an R interface to the JavaScript library DataTables.
library(DT)
datatable(iris)
datatable(head(iris), class = 'cell-border stripe')
double-click a cell to edit its value.
datatable(head(iris), editable = 'cell')
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))
datatable(
head(iris),
caption = 'Table 1: This is a simple caption for the table.'
)
datatable(iris, filter = 'top', options = list(
pageLength = 5, autoWidth = TRUE
))
datatable(head(iris, 30), callback = JS('table.page("next").draw(false);'))
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')
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')
)
)