Introduction
Interactive widgets bring your R documents to life, transforming static outputs into dynamic, dashboard-like experiences. In this tutorial, you’ll learn how to use htmlwidgets—a framework that connects R to JavaScript libraries—to embed interactive tables, maps, and charts directly into your Quarto documents.
1. Overview of Interactive Widgets
Widgets allow users to interact with your data by filtering, sorting, and zooming into outputs. With htmlwidgets, you can integrate many popular libraries into R, including:
- DT: For interactive data tables.
- Leaflet: For interactive maps.
- Plotly: For interactive charts (often used to add interactivity to ggplot2 plots).
2. Using htmlwidgets in R
a. Interactive Data Tables with DT
The DT package transforms R data frames into fully interactive HTML tables.
Display the first 5 rows of the mtcars dataset in an interactive table:
Source
```{webr}
library(DT)
datatable(mtcars, options = list(pageLength = 5))
```
b. Interactive Maps with Leaflet
The leaflet package allows you to create interactive maps with pan, zoom, and marker functionalities.
Source
```{webr}
library(leaflet)
leaflet() %>%
addTiles() %>%
addMarkers(lng = -93.65, lat = 42.0285, popup = "Hello from Leaflet!")
```
c. Enhancing Visualizations with Plotly
Plotly can be used to add interactivity to ggplot2 charts, enabling features like tooltips, zooming, and panning.
Source
```{webr}
library(ggplot2)
library(plotly)
p <- ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Interactive Plotly Visualization",
x = "Weight", y = "Miles per Gallon",
color = "Cylinders")
ggplotly(p)
```
3. Combining Widgets for Dashboards
You can combine multiple widgets to create a comprehensive interactive dashboard. For example, you might display an interactive table alongside a map to provide a multi-faceted view of your data.
Source
```{webr}
library(DT)
library(leaflet)
# Create an interactive data table
output_table <- datatable(head(mtcars, 10), options = list(pageLength = 5))
# Create an interactive map
output_map <- leaflet() %>%
addTiles() %>%
addMarkers(lng = -93.65, lat = 42.0285, popup = "Hello from Leaflet!")
# Display both widgets
output_table
output_map
```
Quarto Live will display these widgets one after the other, forming a simple dashboard layout.
Further Reading
- Interactive Code Blocks Explained
Learn how to create and customize interactive code blocks in Quarto Live. - Loading and Using Packages
Discover techniques for installing and loading additional R packages. - Managing Execution Environments
Understand how to control variable sharing and isolate interactive sessions. - Cell Options Reference
Explore advanced configuration options for interactive code blocks.
Conclusion
Integrating interactive widgets in R with htmlwidgets can elevate your documents by creating dynamic, interactive outputs—whether it’s data tables, maps, or enhanced visualizations. Experiment with these examples, follow best practices, and troubleshoot as needed to build engaging, interactive R dashboards.
Explore More Articles
Here are more articles from the same category to help you dive deeper into the topic.
Reuse
Citation
@online{kassambara2025,
author = {Kassambara, Alboukadel},
title = {Integrating {Interactive} {Widgets} in {R}},
date = {2025-03-16},
url = {https://www.datanovia.com/learn/interactive/r/widgets.html},
langid = {en}
}