flexdashboard icon indicating copy to clipboard operation
flexdashboard copied to clipboard

Output flexdashboard with renderLeaflet to html

Open Otoliths opened this issue 2 years ago • 0 comments

source: Output flexdashboard with renderLeaflet to html

I have flexdashboard code that works when I run it within RStudio (using the "Run Document" button and viewing in RStudio's viewer - and also when clicking the "Open in Browser" button in viewer, it opens a system browser and works fine). However, if I use rmarkdown::render() to output it to an html file, the reactive leaflet maps no longer work and are blank. The maps that are output using leaflet() work, but not renderLeaflet().

for example:

---
title: "Project Dashboard"
output:
flexdashboard::flex_dashboard:
orientation: columns
runtime: shiny
---

```{r setup}
library(tidyverse)
library(sf)
library(leaflet)
library(flexdashboard)
library(shiny)

```{r data}
# Create the data
cities_data <- data.frame(
  Name = c("City A", "City B", "City C"),
  Latitude = c(40.7128, 34.0522, 51.5074),   
  Longitude = c(-74.0060, -118.2437, -0.1278)
  ) %>% 
  st_as_sf(coords = c("Longitude", "Latitude")) %>% 
  st_set_crs(4326)
Column {data-width=100}

```{r}
selectInput("Name", label = "Select City",
            choices = c("\n", unique(as.character(cities_data$Name))),
            selected = "\n",
            width = "100%")
Column {.tabset .tabset-fade}

### Individual Cities

```{r}
# Reactive leaflet map
renderLeaflet({
  
  leaflet() %>%
    addProviderTiles(providers$Esri.WorldImagery,
                     group = "Esri.WorldImagery") %>%
    addCircleMarkers(data = cities_data %>% filter(Name == input$Name),
                     color = "blue")
})
### All Cities

```{r}
## Leaflet map (non-reactive)
Map <- leaflet(data = cities_data) %>% 
  addProviderTiles(providers$Esri.WorldTopoMap)  %>%
  addCircleMarkers(color = "red") 
Map

f I run this example code with rmarkdown::render(input = "xxx.Rmd", output_file = "xxx.html"), the selectable Individual Cities maps do not work in the output html file, but the All Cities map works fine. I am guessing the shiny::selectInput() does not play well with renderLeaflet() in a flexdashboard scenario. Does anyone have a solution for this?

Otoliths avatar Oct 19 '23 16:10 Otoliths