Integrating Interactive Widgets in R

Leveraging htmlwidgets for Dynamic Outputs

Learn how to enhance your R documents with interactive widgets. This tutorial covers how to use htmlwidgets—such as DT for interactive tables and leaflet for maps—to build dynamic dashboards.

Tools
Author
Affiliation
Published

March 16, 2025

Keywords

interactive R widgets, htmlwidgets interactive, R interactive dashboard

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
```
Note

Quarto Live will display these widgets one after the other, forming a simple dashboard layout.

Further Reading

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.

Back to top

Reuse

Citation

BibTeX 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}
}
For attribution, please cite this work as:
Kassambara, Alboukadel. 2025. “Integrating Interactive Widgets in R.” March 16, 2025. https://www.datanovia.com/learn/interactive/r/widgets.html.