0

I have created a multipage flexdashboard app (.Rmd) in R using shiny runtime. One of the pages is a leaflet map with a large polygon layer. The map initially draws as it should but after I browse through the remaining pages and return to the map it doesn't draw. The other pages show up as they should. I have simplified the polygon layer. Reproducible code below:

---
title: "try"
date: "`r Sys.Date()`"
output:
  flexdashboard::flex_dashboard:
  orientation: rows 
  vertical_layout: fill
runtime: shiny
---
  
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo=FALSE)
library(flexdashboard)
library(shiny)
library(tidyverse)
library(sf)
library(leaflet)
library(plotly)

pdf(NULL)

nc <- st_read(system.file("shape/nc.shp", package="sf")) %>% st_transform(4326)

subset <- nc %>% filter(SID74 <=5)

# Colour palettes
colPal <- tibble(
  numbers = c("1", "2", "3", "4"),
  Color = c("#990000", "#CC9966", "#FFFF33", "#99CC66"))

sid1 <- subset %>% filter(SID74 == "1" )
  value1 <- round(sum(sid1$AREA),0)
sid2 <- subset %>% filter(SID74 == "2" )
  value2 <- round(sum(sid2$AREA),0)
sid3 <- subset %>% filter(SID74 == "3" )
  value3 <- round(sum(sid3$AREA),0)
sid4 <- subset %>% filter(SID74 == "4" )
  value4 <- round(sum(sid4$AREA),0) 
  
  # map
  splitField <- "SID79"
years <-  unique(subset[[splitField]])
subset$SID74 <- as.character(subset$SID74)

for (i in 1:length(years)) {
  tmp <- subset[subset[[splitField]] == years[i], ]
  assign(paste0("year", i), tmp)
}
```

```{r reactive}
filtered <- reactive({
  sid74_chosen <- req(input$SID74_select)
  sid79_chosen <- req(input$SID79_select)
  
  if ("All" %in% sid74_chosen) sid74_chosen <- unique(subset$SID74)
  if ("All" %in% sid79_chosen) sid79_chosen <- unique(subset$SID79)
  
 df <-  subset %>%
       filter(SID74 %in% sid74_chosen, SID79 %in% sid79_chosen)
 return(df)
})
```

Interactive Plots
===========================================================

Filters {.sidebar}
-----------------------------------------------------------

```{r}
selectInput(inputId = "SID74_select",
                              label = "SID74",
                              choices = c("All", sort(unique(subset$SID74))),
                              multiple = TRUE,
                              selectize = TRUE,
                              selected = "All")
                              
selectInput(inputId ="SID79_select",
            label ="SID79",
            choices = c("All",sort(unique(subset$SID79))),
            multiple = TRUE,
            selectize = TRUE,
            selected = "All")
```

Row
-----------------------------------------------------------

###

```{r}
#renderValueBox({
   valueBox("ValueBox 1", value = value1, color = "#990000")
#})  
```
###
```{r}
#renderValueBox({
   valueBox("ValueBox 2", value = value2, color = "#CC9966")
#})
```
###
```{r}
#renderValueBox({
   valueBox("ValueBox 3", value = value3, color = "#FFFF33")
#})
```

###
```{r}
#renderValueBox({
  valueBox("ValueBox 4", value = value4, color = "#99CC66")
#})
```

Row {.tabset .tabset-fade}
----------------------------------------------------------------------------

### **Sample plots**

```{r}
# Bar chart

renderPlotly({
 dat <- filtered()
 dat$SID74 <- factor(dat$SID74, levels = colPal$numbers)

  ggplotly(ggplot(data = dat, aes(x=SID79, y=AREA,fill=SID74)) + 
    labs(title = "NC", x ="Sample X", y = "Sample Y", fill = "Fill") +
   stat_summary(fun = "sum", geom = "bar", position = "stack", col = NA) + 
    scale_fill_manual(values = colPal$Color) +
    facet_wrap(~SID79)+
    scale_y_continuous(labels = scales::comma))
})
```
Map
===========================================================
  
Row
-----------------------------------------------------------
### Map

```{r}

 colour6 <- tibble(
    numbers = c("0", "1", "2", "3", "4","5"),
    Color = c('#FFFF33',  '#00CC99', '#006633' , '#99CC99', 
           '#6699CC','#CC9900'))
  
  m1 <- leaflet() %>% 
    setView(lng = -79.5, lat = 36,zoom =7) %>% 
    addTiles(group = "OSM (default)") %>% 
    addProviderTiles("CartoDB.DarkMatter", group = "CartoDB")
  
  for (i in 1:length(years)) {
      dat <- get(paste0("year",i))
       dat_col <- merge( x =dat, y =colour6, by.x ="SID74", by.y="numbers")

  m1 <- m1 %>% addPolygons(data =dat_col,
                   group = paste0(i),
                  fillColor = dat_col$Color,
                  opacity = 1,
                  color =  dat_col$Color,
                  fillOpacity = 0.7)
  }

  m1 <- m1 %>%   addLayersControl(
      baseGroups = c("OSM (default)", "Toner", "CartoDB"),
      options = layersControlOptions(collapsed = FALSE),
      overlayGroups  =  unique(years))  %>%
    addLegend(colors = colour6$Color, labels = colour6$numbers)

renderLeaflet({ 
  m1
})

```
1

0