title: Shiny leaflet map - filtering data by columns, not rows
body: <p>First, I have a perfect solution <a href="https://stackoverflow.com/questions/46186014/changing-leaflet-map-according-to-input-without-redrawing-multiple-polygons">here</a> how to create a Shiny leaflet map and filters based on rows.</p>
  
  <p>Now I want to show a very similar map, where filters are applied to columns. Stumbled a little bit.</p>
  
  <p>I want to create a map of US adult smoking by year, highlighting % with different colors. </p>
  
  <p>Data in .csv format is <a href="https://www.dropbox.com/s/xt7xlfib2jb40le/US%20adult%20smoking%20by%20state1.csv?dl=0" rel="nofollow noreferrer">here</a></p>
  
  <p>Shapefiles are <a href="https://www.dropbox.com/s/fopgl8ejfa3mvv9/cb_2016_us_state_500k.zip?dl=0" rel="nofollow noreferrer">here</a></p>
  
  <p>Currently, my map looks like this</p>
  
  <p><a href="https://i.stack.imgur.com/aOsoG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aOsoG.png" alt="Currently, my map looks like this"></a></p>
  
  <p>This is my code:</p>
  
  <pre><code># Set directory
  setwd("C:/DC/R/Shiny/US Adult Smoking by State")
  
  # Upload packages
  library(ggthemes)
  library(rgdal)
  library(sp)
  library(leaflet)
  library(shinythemes)
  
  # Read dataset
  smoking &lt;- read.csv("US adult smoking by state1.csv", header = TRUE)
  
  # Leaflet map
  states &lt;- readOGR(dsn = "C:/DC/R/Cool datasets/US smoking", layer = 
  "cb_2016_us_state_500k", 
                encoding = "UTF-8", verbose = FALSE)
  
  # Merge data
  # require(sp)! For spatial dataframe!
  smoking.df &lt;- merge(states, smoking, by.x = "NAME", by.y = "state")
  class(smoking.df)
  
  # Create palette
  pal &lt;- colorBin("Reds", c(0, 30), na.color = "#808080",
              alpha = FALSE, reverse = FALSE)
  
  
  # UI
  ui &lt;- shinyUI(fluidPage(theme = shinytheme("united"),
                      titlePanel(HTML("&lt;h1&gt;&lt;center&gt;&lt;font size=14&gt; US Adult 
  Smoking by State in 2015-2017&lt;/font&gt;&lt;/center&gt;&lt;/h1&gt;")), 
                      sidebarLayout(
                        sidebarPanel(
                          selectInput("stateInput", label = h3("State"),
                                      choices = c("Choose state",
                                                  "Alabama",
                                                  "Alaska",
                                                  "Arizona",
                                                  "Arkansas",
                                                  "California",
                                                  "Colorado",
                                                  "Connecticut",
                                                  "Delaware",
                                                  "Florida",
                                                  "Georgia",
                                                  "Hawaii",
                                                  "Idaho",
                                                  "Illinois",
                                                  "Indiana",
                                                  "Iowa",
                                                  "Kansas",
                                                  "Kentucky",
                                                  "Louisiana",
                                                  "Maine",
                                                  "Maryland",
                                                  "Massachusetts",
                                                  "Michigan",
                                                  "Minnesota",
                                                  "Mississippi",
                                                  "Missouri",
                                                  "Montana",
                                                  "Nebraska",
                                                  "Nevada",
                                                  "New Hampshire",
                                                  "New Jersey",
                                                  "New Mexico",
                                                  "New York",
                                                  "North Carolina",
                                                  "North Dakota",
                                                  "Ohio",
                                                  "Oklahoma",
                                                  "Oregon",
                                                  "Pennsylvania",
                                                  "Rhode Island",
                                                  "South Carolina",
                                                  "South Dakota",
                                                  "Tennessee",
                                                  "Texas",
                                                  "Utah",
                                                  "Vermont",
                                                  "Virginia",
                                                  "Washington",
                                                  "West Virginia",
                                                  "Wisconsin",
                                                  "Wyoming"
                                                  ),
                                      selected = "Choose state"),
                          selectInput("stateInput", label = h3("State"),
                                      choices = c("Choose year",
                                                  "2015",
                                                  "2016",
                                                  "2017"),
                                      selected = "Choose year")),
                        mainPanel(leafletOutput(outputId = 'map', height = 
  800) 
                            ))
                      ))
  
  
  
  # SERVER
  server &lt;- shinyServer(function(input, output) {
  output$map &lt;- renderLeaflet({
  leaflet(smoking.df) %&gt;% 
    addProviderTiles(providers$Stamen.TonerLite) %&gt;% 
    setView(lng = -98.583, lat = 39.833, zoom = 4) #%&gt;% 
  
  })
  # observers
  
  # selected state
  selectedState &lt;- reactive({
  smoking.df[smoking.df$NAME == input$stateInput, ] 
  })
  
  observe({
  state_popup &lt;- paste0("&lt;strong&gt;State: &lt;/strong&gt;", 
                        selectedState()$NAME, 
                        "&lt;br&gt;&lt;strong&gt;% of smoking adults in 2015: &lt;/strong&gt;",
                        selectedState()$adult_smoking_2015,
                        "&lt;br&gt;&lt;strong&gt;% of smoking adults in 2016: &lt;/strong&gt;",
                        selectedState()$adult_smoking_2016,
                        "&lt;br&gt;&lt;strong&gt;% of smoking adults in 2017: &lt;/strong&gt;",
                        selectedState()$adult_smoking_2017)
  
  leafletProxy("map", data = selectedState()) %&gt;%
    clearShapes() %&gt;%
    addPolygons(fillColor = "orange",
                popup = state_popup,
                color = "#BDBDC3",
                fillOpacity = 0.8,
                weight = 1)
  })
  
  # selected year
  selectedYear &lt;- reactive({
  smoking.df[smoking.df$adult_smoking_2015 == input$yearInput &amp;
             smoking.df$adult_smoking_2016 == input$yearInput &amp;
             smoking.df$adult_smoking_2017 == input$yearInput,] 
  })
  
  observe({
  state_popup1 &lt;- paste0("&lt;strong&gt;State: &lt;/strong&gt;", 
                        selectedState()$NAME)
  
  leafletProxy("map", data = selectedYear()) %&gt;%
    clearShapes() %&gt;%
    addPolygons(fillColor = ~pal(selectedYear()$yearInput),
                popup = state_popup1,
                color = "#BDBDC3",
                fillOpacity = 0.8,
                weight = 1)
  })
  
  
  })
  
  
  # Run app! 
  shinyApp(ui = ui, server = server)
  </code></pre>
  
  <p>So, my assumption that I am screwing up with inputYear, and also with the color palette in leaflet map. Years are in columns and it is a little bit difficult for me now to understand where is my mistake. </p>
  
  <p>Super grateful for all hints. Hopefully, my question will also help other people.  </p>
  
  <p>My desirable outcome, which I created just in leaflet below. I want to change year in a filter and receive the change on a map.
  <a href="https://i.stack.imgur.com/SrccH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SrccH.png" alt="enter image description here"></a></p>