title: Dynamically render choropleth map with sliderInput in R shiny
body: <p>I have shapefile which I am reading into R using readOGR to convert it to SpatialPolygonDataframe. The attribute table looks as shown in the figure below. </p>
  
  <p><a href="https://i.stack.imgur.com/9wX2Q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9wX2Q.png" alt="enter image description here"></a></p>
  
  <p>Each row is a zone (postal code area) and there are values for each hour of the day eg: h_0, h_1, ...h_23 measured for each zone. In my shiny app I want to show a map which changes as the user select a particular hour using sliderInput widget. The shiny app looks like below:</p>
  
  <p><a href="https://i.stack.imgur.com/a1lnR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/a1lnR.png" alt="enter image description here"></a></p>
  
  <p>The code that produces the above result is here: </p>
  
  <pre><code> library(shiny)
   library(leaflet)
   library(reshape2)
   library(maps)
   library(mapproj)
   library(rgdal)
   library(RColorBrewer)
   library(sp)
   library(rgeos)
  
  
  
  ui &lt;- fluidPage(
  
   titlePanel("Title"),
  
   sidebarLayout(
  
   sidebarPanel(
     tabsetPanel(id= "tabs",
  
                tabPanel("Map", id = "Map", 
                         br(), 
  
                         p("Choose options below to interact with the Map"), 
  
                         sliderInput("hour", "Select the hours", min = 0 , max = 23, 
                                     value = 7, step = 1, dragRange= TRUE)
                         )
    )
  ),
  
  mainPanel(
  
    tabsetPanel(type= "tabs",
  
  
                tabPanel("Map", leafletOutput(outputId = "map"))
            )
    )
   )
   )
  
  
  
   server &lt;- function(input, output) {
  
  
    layer &lt;- reactive( {
  
        shp = readOGR("shp",layer = "attractiveness_day3")
        shp_p &lt;- spTransform(shp, CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
  
    })
  
   output$map &lt;- renderLeaflet({
       bins&lt;- c(0, 2000, 4000, 8000, 16000, Inf)
      pal &lt;- colorBin("YlOrRd", domain = layer()$h_7, bins = bins)
  
      leaflet(layer()) %&gt;% 
      setView(13.4, 52.5, 9) %&gt;% 
      addTiles()%&gt;%
        addPolygons( 
          fillColor = ~pal(h_7),
          weight = 0.0,
          opacity = 1,
          color = "white",
          dashArray = "3",
          fillOpacity = 0.7 
        )  %&gt;% 
        addLegend(pal = pal, values = ~h_7, opacity = 0.7, title = NULL, position = "bottomright")
  
  
    })
    #until here it works but onwards not. 
   observe(leafletProxy("map", layer())%&gt;%
             clearShapes()%&gt;%
             addPolygons( 
               fillColor = ~pal(h_7),  # is it possible here to pass column name dynamically
               weight = 0.0,
               opacity = 1,
               color = "white",
               dashArray = "3",
               fillOpacity = 0.7 
             )  %&gt;% 
             addLegend(pal = pal, values = ~h_7, opacity = 0.7, title = NULL, position = "bottomright")
   )
  
  }
  
  
  shinyApp(ui, server)
  </code></pre>
  
  <p>So currently the choropleth map is populated  with values of column <code>h_7</code> selected statically. But I don't know how and whether I can dynamically pass the column name based on sliderInput selection ( For eg. If sliderInput value is 8 the corresponding column is h_8).  And then render the map based on the selected column passed from reactive funnction to the <code>observe</code> and <code>leafletProxy</code> functions. </p>
  
  <p>sample data : <a href="https://www.dropbox.com/sh/ybziyi1pbdwjkb7/AADNUPHWljy0vS4lH4z-V_v4a?dl=0" rel="nofollow noreferrer">sample data</a></p>