htmlwidgets for R

Overview

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

If you are using 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.

Usage

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

```{r}
library(leaflet)
leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
```
Warning: package 'leaflet' was built under R version 4.1.2

Layout

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

```{r}
#| layout: [[1,1], [1]]
library(dygraphs)
dygraph(fdeaths, "Female Deaths")
dygraph(mdeaths, "Male Deaths")
dygraph(ldeaths, "All Deaths")
```
Female Deaths
400
600
800
1000
1200
Jan 1974
Jul 1974
Jan 1975
Jul 1975
Jan 1976
Jul 1976
Jan 1977
Jul 1977
Jan 1978
Jul 1978
Jan 1979
Jul 1979
Male Deaths
1000
1500
2000
2500
Jan 1974
Jul 1974
Jan 1975
Jul 1975
Jan 1976
Jul 1976
Jan 1977
Jul 1977
Jan 1978
Jul 1978
Jan 1979
Jul 1979
All Deaths
1500
2000
2500
3000
3500
4000
Jan 1974
Jul 1974
Jan 1975
Jul 1975
Jan 1976
Jul 1976
Jan 1977
Jul 1977
Jan 1978
Jul 1978
Jan 1979
Jul 1979

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

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