Search code examples
rshinyr-leaflet

Shiny leaflet select input code for changing circle colors


I want change circles color of selected region on the map. Need your advise. Please help me to create the right observeEvent for selectInput action. I need the app clear my circles and highlight only selected region with specific pop up.

My data:

          Region     Pop  Latitude Lontitude
1         Cherkasy 1238593 49.444433 32.059767
2        Chernihiv 1040492 51.498200 31.289350
3       Chernivtsi  909081 48.292079 25.935837
4     City of Kyiv 2909491 50.450100 30.523400
5           Dnipro 3244341 48.464717 35.046183
6          Donetsk 4255450 48.015883 37.802850
7  Ivano-Frankivsk 1381014 48.922633 24.711117
8          Kharkiv 2711475 49.993500 36.230383
9          Kherson 1059481 46.635417 32.616867
10    Khmelnytskiy 1291187 49.422983 26.987133
11      Kirovohrad  969662 48.507933 32.262317
12            Kyiv 1732435 50.450100 30.523400
13         Luhansk 2200807 48.574041 39.307815
14            Lviv 2531265 49.839683 24.029717
15       Mykolayiv 1155174 46.975033 31.994583
16           Odesa 2386441 46.482526 30.723310
17         Poltava 1433804 49.588267 34.551417
18           Rivne 1161537 50.619900 26.251617
19            Sumy 1108651 50.907700 34.798100
20        Ternopil 1063264 49.553517 25.594767
21       Vinnytsya 1597683 49.233083 28.468217
22           Volyn 1042218 50.747233 25.325383
23     Zakarpattya 1258507 48.620800 22.287883
24    Zaporizhzhya 1747639 47.838800 35.139567
25        Zhytomyr 1244219 50.254650 28.658667

Code

library(shiny)
library(leaflet)
library(maps)
library(shinythemes)
library(readxl)

UkrStat <- read_excel("D:/My downloads/Downloads/R Studio/UkrStat.xlsx")

ui <- (fluidPage(theme = shinytheme("superhero"),
                 titlePanel("Map of Ukraine"),
                 sidebarLayout(
                   sidebarPanel(
                     selectInput("region", label = "Region", choices = c("", UkrStat$Region), selected = "City of Kyiv")
                   ),
                   mainPanel(
                     leafletOutput("CountryMap", width = 1000, height = 500))
                 )
))

server <- function(input, output, session){
  output$CountryMap <- renderLeaflet({
    leaflet() %>% addTiles() %>% addProviderTiles("CartoDB.Positron") %>%
      setView(lng = 31.165580, lat = 48.379433, zoom = 6) %>%
      addCircles(lng = UkrStat$Lontitude, lat = UkrStat$Latitude, weight = 1, radius = sqrt(UkrStat$Pop)*30, popup = UkrStat$Region)
  })

  observeEvent(input$region, {
    leafletProxy("CountryMap") %>% clearMarkers()
  })
}





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

Solution

  • To achieve what you want you need to replace your observeEvent for input$region with the following:

      observeEvent(input$region, {
    
        if(input$region != "")
        {
          leafletProxy("CountryMap") %>% clearShapes()
          index = which(UkrStat$Region == input$region)
          leafletProxy("CountryMap")%>% addCircles(lng = UkrStat$Lontitude[index], lat = UkrStat$Latitude[index],
                                                   weight = 1, radius = sqrt(UkrStat$Pop[index])*30, popup = UkrStat$Region[index])
        }
      }) 
    

    Here I am first clearing all the circles. After that I am finding the index for the selected region and getting the corresponding longitude and latitude and adding circles at that position.

    Hope it helps!