0

Using the renderLeaflet (following codes), I am trying to create an interactive map within shiny. I want the map to be updated based on users selections of age group, sex, and year. When age group=60 and sex=both sexes and year= 2010 are selected, everything looks great (please see this screen shot). However, when the other age groups are selected, the legend remains unchanged and is not rendered (Please see the second screen shot). Here is my code:

mapdata_ <- reactive ({

    nhmap$Per <- round(nhmap$Per, 1) 

    out_map <- nhmap %>%
        filter (
            Age_Group %in% input$Age_Group_map,
            Sex %in% input$sex_map,
            Year %in% input$Year_map)

    return(out_map)
})

output$int_map <- renderLeaflet ({

    leaflet (mapdata_(),
             pal8 <- c("#FFFFE5", "#D9F0A3", "#78C679", "#006837") ,
             pal <- colorBin(palette = pal8, domain = nhmap$Per, bins=4, right =FALSE, na.color = "#808080",  alpha = FALSE, reverse = F)
            ) %>%

    addProviderTiles("CartoDB.Positron") %>% 
    clearControls() %>%
    clearShapes()%>%
    addPolygons(fillColor = ~pal(Per),
                stroke=T,
                weight=1,
                smoothFactor=0.2,
                fillOpacity = 1,
                color="black",
                popup=~paste(NAME,"<br>",input$sex_map,
                    input$Age_Group_map,"=",Per,"%"),
                highlightOptions = highlightOptions(color = "red",
                    weight = T,
                    bringToFront = T),
                label=~NAME) %>%
    addTiles() %>%
    setView(-82.706838, 40.358615, zoom=7) %>%
    addLegend(position = "bottomright",
        values = ~pal(Per),
        pal = pal,
        title = (paste("%",input$Age_Group_map, input$sex_map, "in", input$Year_map)) ,
        labFormat = labelFormat())
})
AS Mackay
  • 2,831
  • 9
  • 19
  • 25
Nader Mehri
  • 514
  • 1
  • 5
  • 21
  • Try `values = ~Per` when you build the legend--you're creating values directly from the data, not with the palette as a middleman – camille Nov 04 '18 at 13:26
  • Thanks. It did not work; I got the same results. Also, I tried values=nhmap$Per, but no changes happened. – Nader Mehri Nov 04 '18 at 14:54

0 Answers0