I would like to make a shiny app where the coloring of polygons depends on the clicked polygon. I can get it to work when everything is static but using reacitve values kinda trips me up.
Essentially, what I think I have to do is to set the domain argument of the palette and the fill color argument as reactive values.
stgt.counts is my shapefile.
app <- shinyApp(
ui = bootstrapPage(
leafletOutput('map'),
textOutput('text')
),
server = function(input, output) {
filtered.zone <- reactive({
paste0("count", input$map_shape_click$id)
})
filtered.domain <- reactive({
paste0("stgt.counts$count", input$map_shape_click$id)
})
output$map <- renderLeaflet({
zone <- filtered.zone()
domain <- filtered.domain()
bins <- c(0,1,2,5,6,7,8)
pal <- colorBin("YlOrRd", domain = domain, bins = bins)
m <- leaflet(stgt.counts) %>%
addProviderTiles(providers$Stamen.Toner)
m %>% addPolygons(
fillColor = ~pal(zone),
weight = 2,
layerID = ~NO,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.7
)
})
output$text <- renderText({
if (is.null(input$map_shape_click$id)) return()
print(input$map_shape_click$id)
})
}
)
runApp(app)
The current warning I get is "Warning: Error in cut.default: 'x' must be numeric' - Which I suppose is because zone and domain are strings. Is that right? As this is my first shiny app using reactives I'm guessing there's a much better way to do this. Any help is highly appreciated.