2

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)
Oscar Jnn
  • 154
  • 1
  • 19

1 Answers1

2

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!

Oscar Jnn
  • 154
  • 1
  • 19
LocoGris
  • 4,432
  • 3
  • 15
  • 30
  • Thanks for your reply, i've edited my question and my code with some data. It doesn't seem to work i get this error message when i run the code "Warning: Error in <-: cannot add bindings to a locked environment [No stack trace available] Warning: Error in classInt::classIntervals: var is not numeric [No stack trace available]" Any idea? – Oscar Jnn Mar 14 '19 at 16:46
  • 2
    Solved! the extra problem came from reactivity : v <- reactiveValues(data = commData$Mobilite) observeEvent(input$radio,{ if(input$radio=="Attr1"){ v$data <- commData$Mobilite } if(input$radio=="Attr2"){ v$data <- commData$Dependance } }) I've edited your answer – Oscar Jnn Mar 15 '19 at 10:30