htmlwidgets for R


The htmlwidgets package enables you to use JavaScript visualization libraries like Leaflet, Plotly, dygraphs, and threejs directly from R.

If you are uisng the Knitr engine with Quarto this is a great way to incorporate interactivity without learning JavaScript or requiring a Shiny Server to view your document.


Including htmlwidgets within a Quarto document is as easy as including an R plot. For example, here is how we embed a Leaflet map:

leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")


You can also use layout options with htmlwidgets. For example, here we provide a custom layout to arrange three dygraph time series plots:

#| layout: [[1,1], [1]]
dygraph(fdeaths, "Female Deaths")
dygraph(mdeaths, "Male Deaths")
dygraph(ldeaths, "All Deaths")

See the article on Figures & Layout for additional documentation on custom layouts.

To learn about available htmlwidgets see the showcase page and the htmlwidget gallery.