首页 > 解决方案 > Using renderLeaflet() inside an interactive document (Rmarkdown)

问题描述

I'm trying to create an interactive document like the tutorial on Shiny's website. However, I want the plot to be a Leaflet map. Following the Leaflet tutorial, it seems like the following should work using leaflet::renderLeaflet() instead of shiny::renderPlot().

Instead, I get an error when running the document: Error: object 'input' not found. Is there a special way to expose input to leaflet when using Shiny widgets inside an interactive Rmarkdown document?

---
runtime: shiny
output: html_document
---


```{r echo = FALSE}
library(magrittr)
shiny::selectInput(
  "weight", label = "Weight:",
  choices = c(1, 2, 3, 4, 5), selected = 5
)
```

```{r include = FALSE}
csa <- tigris::combined_statistical_areas(class = "sf")
```

```{r echo = FALSE}
leaflet::renderLeaflet({   # <- main difference from tutorial
  leaflet::leaflet() %>%
    leaflet::addTiles() %>%
    leaflet::addPolylines(data = csa, color = '#333', weight = input$weight)
})
```

标签: rshinyleaflet

解决方案


I can't confirm your issue. Your sample interactive document runs just fine:

enter image description here

I've been using the following library versions on R 3.6.1

  • leaflet_2.0.2
  • rmarkdown_1.16
  • shiny_1.4.0

推荐阅读