rr-leafletquarto

How to include a leaflet map in Quarto closeread document?


I'm making a Quarto document with the closeread "scrollytelling" extension. Right now, when I attempt to create a leaflet map from an R code chunk, the resulting HTML file does not show the map. Do you know what I'm doing wrong?

Example:

---
title: "Untitled"
format: closeread-html
---

:::{.cr-section}

:::{#cr-leaflet}
```{r}
library(leaflet)
leaflet() |>
  addTiles()
```
:::

Lorem Ipsum. @cr-leaflet

:::

This results in a blank document like this, without the expected leaflet map.

screenshot of resulting quarto doc


Solution

  • Exporting the map as a standalone HTML-widget and then embedding it works.

    ---
    title: "Untitled"
    format: closeread-html
    ---
    
    :::{.cr-section}
    
    :::{#cr-leaflet}
    ```{r}
    library(leaflet)
    
    map <- leaflet() %>% addTiles()
    temp <- tempfile(fileext = ".html") # temporary file
    htmlwidgets::saveWidget(map, temp, selfcontained = TRUE)
    htmltools::includeHTML(temp)
    unlink(temp) # unlink temp file
    ```
    :::
    
    Lorem Ipsum. @cr-leaflet
    
    :::
    

    out