Show the code
install.packages("DT")
library(DT)
- 1
- Install the DT package if not already installed
- 2
- Load the DT package for creating interactive tables
The DT
package in R makes it easy to create interactive tables that work inside websites, Quarto documents, and Shiny apps. These tables let users search, sort, filter, paginate, and even download the data, all through a web-based interface.
DT is a wrapper around the JavaScript DataTables library, but you don’t need to know any JavaScript — it works directly with your R data frames or tibbles.
Install and load the DT package:
install.packages("DT")
library(DT)
Use the datatable()
function to turn a data frame into an interactive table.
datatable(mtcars)
You can add filters to each column using the filter
argument.
%>%
mtcars select(1:4) %>%
datatable(filter = "top")
Use the colnames
argument to rename the columns.
datatable(mtcars,
colnames = c("Miles/Gallon", "Cylinders", "Displacement", "Horsepower",
"Rear Axle Ratio", "Weight", "1/4 Mile Time",
"V/S", "Transmission", "Gears", "Carburetors"))
Colour bars make it easy to visually compare values.
%>%
mtcars select(1:4) %>%
datatable() %>%
formatStyle(
"mpg",
background = styleColorBar(range(mtcars$mpg), "lightblue"),
backgroundSize = "100% 90%",
backgroundRepeat = "no-repeat"
)
Add interactive features with extensions
.
%>%
mtcars select(1:4) %>%
datatable(extensions = c("Buttons", "ColReorder", "FixedHeader", "Scroller", "KeyTable"),
options = list(
dom = 'Bfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
colReorder = TRUE,
fixedHeader = TRUE,
scrollY = 300,
scroller = TRUE,
keys = TRUE
))
library(gapminder)
library(dplyr)
library(DT)
<- gapminder %>%
gap_enhanced filter(continent == "Europe") %>%
mutate(
year = as.factor(year),
country_html = paste0("🌍", country)
%>%
) mutate(country_html = as.factor(country_html)) %>%
select(country_html, year, lifeExp, pop, gdpPercap)
datatable(
gap_enhanced,colnames = c("Country", "Year", "LifeExp", "Population", "GDP per capita"),
escape = FALSE,
extensions = c('Buttons', 'ColReorder', 'FixedHeader', 'Scroller', 'KeyTable'),
options = list(
dom = 'Bfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
colReorder = TRUE,
fixedHeader = TRUE,
scrollY = 400,
scroller = TRUE,
keys = TRUE,
pageLength = 15,
order = list(list(1, 'desc')),
searchCols = list(NULL, list(search = "2007"), NULL, NULL, NULL)
),filter = 'top',
rownames = FALSE,
caption = '📊 Gapminder data for European countries — comparing life expectancy, population, and GDP per capita'
%>%
) formatStyle(
'lifeExp',
background = styleColorBar(range(gap_enhanced$lifeExp), 'lightblue'),
backgroundSize = '100% 90%',
backgroundRepeat = 'no-repeat'
%>%
) formatStyle(
'pop',
background = styleColorBar(range(gap_enhanced$pop), 'pink'),
backgroundSize = '100% 90%',
backgroundRepeat = 'no-repeat'
%>%
) formatStyle(
'gdpPercap',
background = styleColorBar(range(gap_enhanced$gdpPercap), 'lightgreen'),
backgroundSize = '100% 90%',
backgroundRepeat = 'no-repeat'
%>%
) formatRound(c('lifeExp', 'pop'), digits = c(1, 0)) %>%
formatCurrency('gdpPercap', currency = '$', digits = 0)
gapminder
package, which contains country-level statistics over time.
dplyr
, a package for data manipulation.
DT
, which lets you create interactive HTML tables in R.
year
column to a factor so it displays as a dropdown filter.
Year
column to default to 2007.
lifeExp
column.
pop
column.
gdpPercap
column.