How to make a reactive shiny map with slide bars in R

shiny
leaflet
rstudio

#1

0
down vote
favorite
2

I have a question regarding the development of an interactive shiny app that enables users to check which municipality in the Netherlands has the highest ‘score’ according to different “quality of life dimensions”. Ideally, the result is an interactive map that shows five different slide bars with a scale from 1 to 5. These slide bars represent five dimensions which the user can drag to set his preferred weight value per dimension. The application should then show automatically (or using the find button) a map with the municipalities that have the highest ‘score’ based on these weight values. The dataset consists of a shapefile called MunScores2016 that contains all municipalities borders and related scores. These scores are expressed in negative and positive percentages (i.e. ‘0.13’) that show the percentage difference from the national average.

I am struggling to make the map reactive to the user input, anyone has an idea? So far I have come up with:

ui.R :

library(shiny)
library(leaflet)

fluidPage(
  titlePanel("Quality-of-life-o-meter of The Netherlands"),

  sidebarLayout(
    sidebarPanel(
       h3("Dimensions"),
       h6("Assign a weight value for every dimension and press the 'find!' button"),
       sliderInput("housingslider", 
                   label = h4("Housing"), 
                   min = 0, 
                   max = 5, 
                   value = 0),
       sliderInput("populationslider", 
                   label = h4("Population"), 
                   min = 0, 
                   max = 5, 
                   value = 0),
       sliderInput("provisionsslider", 
                   label = h4("Provisions"), 
                   min = 0, 
                   max = 5, 
                   value = 0),
       sliderInput("safetyslider", 
                   label = h4("Safety"), 
                   min = 0, 
                   max = 5, 
                   value = 0),
       sliderInput("physcialenvslider", 
                   label = h4("Physical Environment"), 
                   min = 0, 
                   max = 5, 
                   value = 0),
       actionButton("action", label = "Find!")
  ),
    mainPanel(
      leafletOutput("mymap")
    )
  )
)

server.R

function(input, output, session) {
  output$mymap <- renderLeaflet({
    #Create interactive map
    leaflet(data = MunScores2016) %>% addTiles() %>%
      addPolygons(fillColor = ~pal(Total_Score_2016), 
                  fillOpacity = 1, 
                  color = 'white', 
                  weight = 1,
                  popup = popup_dat) %>%
      addLegend("bottomright", # Legend position
                  pal=pal, # color palette
                  values=~Total_Score_2016, # legend values
                  opacity = 0.7,
                  title="Percentage difference from national average")
  })  
    observe({

      housing <- input$housingslider
      population <- input$populationsliderslider
      provisions <- input$provisionsslider
      safety <- input$hlusingslider
      physicalenvironment <- input$physicalenvslider
    })
    renewedvalue <- function(input)({
      return ((housing * MunScores2016$Housing_Score_2016 +
                 population * MunScores2016$Population_Score_2016 +
                 provisions * MunScores2016$Provisions_Score_2016 +
                 safety * MunScores2016$Safety_Score_2016 +
                 physicalenvironment * MunScores2016$PhysicalEnvironment_Score_2016)/
                 (housing + population + provisions + safety + physicalenvironment))  
    })

}