rshinyr-leafletchoropleth

Shiny reactive value with radiobutton


I am creating a Shiny app meant to display a choropleth map.

I would like to change reactivly the values to show on my map with radiobuttons.

It worked fine with normal button, but i am struggling with the radiobutton now, any ideas?

you can download the data via this link : https://www.data.gouv.fr/fr/datasets/r/8b004f01-e7af-40d2-ab4a-8108a8bd24b2

here is my code so far:

sData <- readOGR(dsn = " Set your path to the downloaded Data here")

ui<- bootstrapPage(
  leafletOutput("mymap"),
  absolutePanel(top = 10, 
                right = 10,
                radioButtons("radio", h3("Indicateurs de mobilité"),
                             choices = list("AttrOne" = "Attr1",
                                            "AttrTwo" = "Attr2")))
  )

server <- function(input, output, session) {
  v <- reactiveValues(data = sData$numdep)
  
  observeEvent(input$Attr1, {
    v$data <- sData $numdep
  })
  observeEvent(input$Attr2, {
    v$data <- sData $insee
  })
 


  output$mymap <- renderLeaflet({
    leaflet(sData) %>%  
      addProviderTiles(
           providers$"CartoDB.DarkMatter") %>%
      addPolygons(
           fillColor = ~colorBin(palette = "YlOrRd", 
                                 bins = getBreaks(v$data, 
                                                  nclass = 6, 
                                                  method = "fisher-jenks"),
                                 domain = v$data
                                 )(v$data),
           weight = 1,
           opacity = 0.3,
           color = "white",
           fillOpacity = 0.3)
  })
}

shinyApp(ui = ui, server = server)

Solution

  • At least one issue is that you are "listening" wrong your button, it should be the id you determined in you ui, in your case, "radio". Also, you can do the you with one observeEvent instead of your way;

        ui<- bootstrapPage(
          leafletOutput("mymap"),
          absolutePanel(top = 10, 
                        right = 10,
                        radioButtons("radio", h3("Indicateurs de mobilité"),
                                     choices = list("AttrOne" = "Attr1",
                                                    "AttrTwo" = "Attr2",
                                                    "AttrThree" = "Attr3")))
          )
    
        server <- function(input, output, session) {
      v <-  observeEvent(input$radio,{ 
    
         if(input$radio=="Attr1"){
             v$data <- Df$A1}
         if(input$radio=="Attr2"){
             v$data <- Df$A2}
         if(input$radio=="Attr3"){
            v$data <- Df$A4}
      })
    
    
    
    
    
      output$mymap <- renderLeaflet({
        leaflet(Df) %>%  
          addProviderTiles(
               providers$"CartoDB.DarkMatter") %>%
          addPolygons(
               fillColor = ~colorBin(palette = "YlOrRd", 
                                     bins = getBreaks(v$data, 
                                                      nclass = 6, 
                                                      method = "fisher-jenks"),
                                     domain = v$data
                                     )(v$data),
               weight = 1,
               opacity = 0.3,
               color = "white",
               fillOpacity = 0.3)
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    Despite of this bug, I cannot assure you this is going to work fine, because your example is not reproducible. Next time, please, may you provide us with a dput(Df) or at least some lines so we can execute your code from start to end and find any other problems?

    WIth your code:

    ui<- bootstrapPage(
      leafletOutput("mymap"),
      absolutePanel(top = 10, 
                    right = 10,
                    radioButtons("radio", h3("Indicateurs de mobilité"),
                                 choices = list("AttrOne" = "Attr1",
                                                "AttrTwo" = "Attr2")))
      )
    
        server <- function(input, output, session) {
      v <- reactiveValues(data = sData$numdep)
    
    observeEvent(input$radio,{ 
    
        if(input$radio=="Attr1"){
          v$data <- sData$numdep
          }
        if(input$radio=="Attr2"){
          v$data <- sData$insee
          }
      })
    
       output$mymap <- renderLeaflet({
        leaflet(sData) %>%  
          addProviderTiles(
               providers$"CartoDB.DarkMatter") %>%
          addPolygons(
               fillColor = ~colorBin(palette = "YlOrRd", 
                                     bins = getBreaks(v$data, 
                                                      nclass = 6, 
                                                      method = "fisher-jenks"),
                                     domain = v$data
                                     )(v$data),
               weight = 1,
               opacity = 0.3,
               color = "white",
               fillOpacity = 0.3)
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    Best!