Search code examples
rshinyr-markdownr-leaflet

R Markdown flexdashboard shiny leaflet app


I'm trying to create an interactive leaflet map dashboard using R markdown and shiny runtime. I need to select multiple values from two fields based on which my map will draw. This works so far, but I want to provide a 'Select All' option for both fields. I have added 'All' in the list of choices in code below, but I don't know how to code further for actually selecting everything in the list. Reproducible R markdown code below:

---
title: "try"
date: "`r Sys.Date()`"
output:
  flexdashboard::flex_dashboard:
  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)

pdf(NULL)

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

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

```

Map
===========================================================
  
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")
```

```{r}
mapFiltered <- reactive({

    filtered_all <- subset %>%
    filter(SID74 %in% input$SID74_select) %>%
    filter(SID79 %in% input$SID79_select)

  return(filtered_all)
})

```
Row
-----------------------------------------------------------
### Map

```{r}
renderLeaflet({

  if (nrow(mapFiltered()) == 0) {
    return(NULL)
  }
  
  dat_map <- mapFiltered()

  leaflet() %>% 
    setView(lng = -79.5, lat = 36,zoom =7) %>%
    addTiles(group = "OSM (default)") %>% 
    addPolygons(data = dat_map)
})

```

Solution

  • You could an if to check whether the All option was chosen:

    ---
    title: "try"
    date: "`r Sys.Date()`"
    output:
      flexdashboard::flex_dashboard:
      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)
    
    nc <- st_read(system.file("shape/nc.shp", package = "sf"))
    
    subset <- nc %>% filter(SID74 <= 5)
    ```
    
    Map
    ===========================================================
      
    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"
    )
    ```
    
    ```{r}
    mapFiltered <- 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)
      
      subset %>%
        filter(SID74 %in% sid74_chosen, SID79 %in% sid79_chosen)
    })
    ```
    Row
    -----------------------------------------------------------
    ### Map
    
    ```{r}
    renderLeaflet({
      if (nrow(mapFiltered()) == 0) {
        return(NULL)
      }
    
      dat_map <- mapFiltered()
    
      leaflet() %>%
        setView(lng = -79.5, lat = 36, zoom = 7) %>%
        addTiles(group = "OSM (default)") %>%
        addPolygons(data = dat_map)
    })
    ```
    

    enter image description here