Hi soedr, thanks a lot for your reply! I'll have a look at your solution. In the meantime I got another suggestion of someone else which also seems to work fine:
server <- function(input, output, session) {
# Create Map once.
output$mymap <-renderLeaflet({
leaflet(data = MunScores2016) %>%
addTiles() %>%
addPolygons(
fillColor = ~pal(Total_Score_2016),
fillOpacity = 1,
color = 'white',
weight = 1,
popup = popup_dat) %>%
addLegend(
"bottomright",
pal=pal,
values=~Total_Score_2016,
opacity = 0.7,
title="Percentage difference from national average")
})
observeEvent(input$action, {
Total_Score <- NA
Total_Score <- ((input$housingslider * MunScores2016$Housing_Score_2016 +
input$populationslider * MunScores2016$Population_Score_2016 +
input$provisionsslider * MunScores2016$Provisions_Score_2016 +
input$safetyslider * MunScores2016$Safety_Score_2016 +
input$physicalenvslider * MunScores2016$PhysicalEnvironment_Score_2016)/
(input$housingslider + input$populationslider + input$provisionsslider + input$safetyslider + input$physicalenvslider))
# Just update the existing map
leafletProxy("mymap") %>%
# This you have to do now: remove the existing polygons, before you paint new ones.
clearShapes() %>%
addPolygons(
data = MunScores2016,
fillColor = ~pal(Total_Score),
fillOpacity = 1,
color = 'white',
weight = 1,
popup = paste0("<strong>Municipality:</strong>",
MunScores2016$Municipality_Name,
"<br><strong>Quality-of-life-o-meter says: </strong>",
Total_Score))
# As you can see, no addLegend is needed, since the legend does not change.
})