Reputation: 1014
I'm trying to build an app which plot choropleth leaflet map based on variable inputed by user.
For now I want to make it work with categorical variables, so I have tried using colorFactor()
to create the palette. The polygons are plotted, but without the colors.
The app is online here and the full code is on a github repo. The server code, where i believe is the error, is below:
function(input, output, session) {
## Interactive Map ###########################################
# Create pallete
colorpal <- reactive({
colorFactor("viridis", as.data.frame(ubs_malhas)[ubs_malhas$modelo == input$modelo, input$indicador])
})
indic.select <- isolate(switch(input$indicador,
vars_setor))
# Create lables
labels_setor <- reactive({
sprintf(
"<strong>%s</strong><br/>
CNES: %s<br/>
Tempo médio até UBS: %g<br/>
Distância média até UBS: %g<br/>
Percetual AV: %s<br/>
AC: %g",
ubs_malhas$nomeubs[ubs_malhas$modelo == input$modelo],
ubs_malhas$cnes[ubs_malhas$modelo == input$modelo],
round(ubs_malhas$media_minutos_ubs[ubs_malhas$modelo == input$modelo], 1),
round(ubs_malhas$media_minutos_ubs[ubs_malhas$modelo == input$modelo], 1),
scales::percent(round(ubs_malhas$av_prop_ubs[ubs_malhas$modelo == input$modelo], 3)),
round(ubs_malhas$ac_ubs[ubs_malhas$modelo == input$modelo], 2)
) %>% lapply(htmltools::HTML)
})
# plot the map
output$mapa <- renderLeaflet({
pal <- colorpal()
leaflet() %>%
addTiles() %>%
setView(lng = -46.64803, lat = -23.64992, zoom = 11)
})
# observe to add polygons and colors
observe({
pal <- colorpal()
var <- input$indicador
leafletProxy("mapa", data = st_transform(ubs_malhas, 4326)[ubs_malhas$modelo == input$modelo, ]) %>%
clearShapes() %>%
addPolygons(
fillColor = pal(var),
weight = 1,
opacity = 0.8,
color = "black",
fillOpacity = 0.6,
# adicionar interação
highlight = highlightOptions(
weight = 3,
color = "#666",
fillOpacity = 0.6,
bringToFront = FALSE),
# adicionar pop-up
label = labels_setor(),
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")
)
})
# observe to add legend
observe({
pal <- colorpal()
leafletProxy("mapa", data = st_transform(ubs_malhas, 4326)[ubs_malhas$modelo == input$modelo, ]) %>%
addLegend(
data = st_transform(ubs_malhas, 4326)[ubs_malhas$modelo == input$modelo, ],
title = names(vars_setor[vars_setor == input$indicador]),
pal = pal,
values = ~input$indicador,
opacity = 0.7,
position = "topleft"
)
})
}
After runApp()
i got the following error:
Listening on http://127.0.0.1:6204
Warning in pal(var) :
Some values were outside the color scale and will be treated as NA
Warning in pal(v) :
Some values were outside the color scale and will be treated as NA
The map is there, but the colors are not.
How could I make it works?
Thanks in advance.
Upvotes: 0
Views: 2041
Reputation: 1
I faced the exact issue. I spent an entire night debugging this but the problem lied in the variable which I passed to pal function. I misspelled a word "North-West" as "North-west". So I'd suggest you to go through the unique values which you are passing to pal and check whether it is aligning with variables used while initializing pal.
Upvotes: 0
Reputation: 7469
Your logical slicer for your function colorpal
is malformed, it should be:
colorFactor("viridis", as.data.frame(ubs_malhas)[ubs_malhas$modelo == input$modelo, ], input$indicador)
Upvotes: 0