Search code examples
rshinyr-leaflet

How to build a dynamic Leaflet Map in R Shiny?


I am trying to build a shiny app where a user can select State location from drop down and then based on those the lat & long should be plotted on the leaflet map.

I have looked at several SO post like Shiny dropdown menu selection to filter a dataframe and shiny tutorials https://shiny.rstudio.com/tutorial/written-tutorial/lesson6/ but nothing has completely worked to make it dynamic based on dropdown selection.

I have also tried using reactive() in my latest code attempt which I have posted below.

library(shiny)
library(shinydashboard)
library(tidyverse)
library(leaflet)
library(readxl)
library(RCurl)

URL <- "https://www.mohfw.gov.in/pdf/PMJAYPRIVATEHOSPITALSCONSOLIDATED.xlsx"
download.file(URL, destfile = "../../timesnow_PMJAYPRIVATEHOSPITALSCONSOLIDATED.xlsx",method = "curl")

# Data
ind_vaccination_center <- readxl::read_xlsx(path = "../../timesnow_PMJAYPRIVATEHOSPITALSCONSOLIDATED.xlsx",
                                             sheet = 1)

# Creating column with html tags for popup & saving in new df
ind_vaccination_leaflet <- ind_vaccination_center %>% 
    mutate(label_display = paste(
        "<h2>", ind_vaccination_center$`Name of the Vaccination Site*`, "</h2>",
        "<h4>",ind_vaccination_center$`District*`,",", ind_vaccination_center$`State*`, "</h4>",
        "<p>", "Address: ", ind_vaccination_center$Address,",", ind_vaccination_center$`PinCode*`, "</p>",
        "<p>", "Mobile: ", ind_vaccination_center$`Mobile Number`, "</p>",
        "<p>", "Contact Person: ", ind_vaccination_center$`Contact Person`, "</p>"
    )
    )

# Define UI for application
ui <- fluidPage(

    # Application title
    titlePanel("Covid19 Vaccination Centers in India"),

    # Sidebar with Dropown selection 
    sidebarLayout(
        
        sidebarPanel(
            selectInput(inputId = "state_selection",
                        label = "Select State", 
                        choices = ind_vaccination_center$`State*`),
            
            h3("List of Vaccination Centers is plotted on Map & also listed in searchable table."),
                     
                     "source of list:",
                     a("https://www.timesnownews.com/india/article/covid-19-vaccination-in-uttar-pradesh-check-complete-list-of-govt-and-private-hospitals-for-jab/726412"),
                     br(),
                     br(),

                     a("https://www.oneindia.com/india/full-list-of-private-hospitals-where-the-covid-19-vaccine-will-be-administered-3223706.html"),
                     br(),
                     br(),
                     "P.S - There might be more center's added to this list, kindly recheck from other sources as well like:",
                     br(),
                     a("https://www.cowin.gov.in/home")
                     ),
        

        # Show a map
        mainPanel(
            leafletOutput("map",height = 800, width = "100%")
        )
    )
)

# Define server logic
server <- function(input, output) {
    
    dataInput <- reactive({
        ind_vaccination_leaflet <- ind_vaccination_leaflet %>% 
            filter(`State*` == input$state_selection)
    })
    
    
    output$map <- renderLeaflet({
        
        # Creating map object & adding layers
        leaflet(dataInput()) %>%
            
            setView(lat = 26.64510, lng = 80.17012, zoom = 4) %>%
            addTiles(group = "OSM") %>%
            addProviderTiles(providers$CartoDB.DarkMatter, group = "Dark") %>%
            addProviderTiles(providers$CartoDB.Positron, group = "Light") %>%
            addProviderTiles("Stamen.Terrain", group = "Terrain") %>%
            addProviderTiles("Esri.WorldImagery", group = "WorldImagery") %>%
            addLayersControl(baseGroups = c("OSM","WorldImagery","Dark","Light","Terrain")) %>%
            addCircleMarkers(
                lng = ind_vaccination_leaflet$`Longitude*`,
                lat = ind_vaccination_leaflet$`Latitude*`,
                label = lapply(ind_vaccination_leaflet$label_display, htmltools::HTML),
                color = "midnightblue",
                weight = 1,
                radius = 8
            )%>%
            addMiniMap(tiles = providers$OpenStreetMap, width = 120, height=80)
        
    })
}



# Run the application 
shinyApp(ui = ui, server = server)


Solution

  • Change the server logic to :

    server <- function(input, output) {
    
      output$map <- renderLeaflet({
        
        req(input$state_selection)
        data <- ind_vaccination_leaflet %>% filter(`State*` == input$state_selection)
        
        
        leaflet(data) %>%
          setView(lat = 26.64510, lng = 80.17012, zoom = 4) %>%
          addTiles(group = "OSM") %>%
          addProviderTiles(providers$CartoDB.DarkMatter, group = "Dark") %>%
          addProviderTiles(providers$CartoDB.Positron, group = "Light") %>%
          addProviderTiles("Stamen.Terrain", group = "Terrain") %>%
          addProviderTiles("Esri.WorldImagery", group = "WorldImagery") %>%
          addLayersControl(baseGroups = c("OSM","WorldImagery","Dark","Light","Terrain")) %>%
          addCircleMarkers(
            lng = ~`Longitude*`,
            lat = ~`Latitude*`,
            label = lapply(data$label_display, htmltools::HTML),
            color = "midnightblue",
            weight = 1,
            radius = 8
          )%>%
          addMiniMap(tiles = providers$OpenStreetMap, width = 120, height=80)
        
      })
    }