ardaar
ardaar

Reputation: 1264

How to insert saved Leaflet widget into Rmarkdown HTML output

I create a Leaflet widget and save it locally:

library(htmlwidgets)
library(leaflet)
library(sf)

shp = st_read("/path/to/some/shapefile.shp")

m = shp %>%
  leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  setView(lng = -70, lat = 40, zoom = 11)

saveWidget(m, "m.html")

Now I want to load this widget in a Rmarkdown chunk:

---
title: "Title"
author: "author"
date: "5/8/2020"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

etc etc etc

```{r}
function_that_loads_widget("m.html")
```

etc etc etc

I've tried htmltools::includeHTML() but that makes the entire HTML output one big widget. The text of the report is not displayed.

I realize I could put the the code that created the Leaflet widget directly in the Rmarkdown chunk, but I don't want to do that.

Upvotes: 4

Views: 989

Answers (1)

Zoltan
Zoltan

Reputation: 782

knitr::include_url() appears to be the solution. This works for my blogdown post.

```{r, out.width="100%"}
knitr::include_url("url_of_html", height="1080px")
```

Upvotes: 2

Related Questions