title: Getting Leaflet to work with Datatable selection in R shiny
body: <p>I have a Massmpg dataset just like the mpg dataset from ggplot but with the addition of City (from Massachusetts) , lat, and lng attributes.  I have made  shiny app which lets me select the city in a datatable but I cannot figure out how to get the leaflet map to focus only on the lat and lng in that city.  Instead the map just shows all the locations in the state.</p>
  <p>Here is the code</p>
  <pre><code>library(shiny)
  library(DT)
  library(leaflet)
  Massmpg &lt;- (read.csv(&quot;Massmpg.csv&quot;))
  
  ui &lt;- fluidPage(titlePanel(&quot;Mass mpg by location&quot;),
                  
                  # Create a new Row in the UI for selectInputs
                  fluidRow(
                      column(4,
                             selectInput(&quot;City&quot;,
                                         &quot;City:&quot;,
                                         c(&quot;All&quot;,
                                           unique(as.character(Massmpg$City))))
                      ),
                      column(4,
                             selectInput(&quot;cyl&quot;,
                                         &quot;cyl:&quot;,
                                         c(&quot;All&quot;,
                                           unique(as.character(Massmpg$cyl))))
                      ),
                      column(4,
                             selectInput(&quot;trans&quot;,
                                         &quot;trans:&quot;,
                                         c(&quot;All&quot;,
                                           unique(as.character(Massmpg$trans))))
                      )
                  ),
                  # Create a new row for the table.
                  leafletOutput(&quot;map01&quot;),
                  DT::dataTableOutput(&quot;table&quot;)
                  
  )
  
  server &lt;- function(input, output) {
          
          # Filter data based on selections
          output$table &lt;- DT::renderDataTable(DT::datatable({
              data &lt;- Massmpg
              if (input$City != &quot;All&quot;) {
                  data &lt;- data[data$City == input$City,]
              }
              if (input$cyl != &quot;All&quot;) {
                  data &lt;- data[data$cyl == input$cyl,]
              }
              if (input$trans != &quot;All&quot;) {
                  data &lt;- data[data$trans == input$trans,]
              }
              data
          }))
          
          # map
          output$map01 &lt;- renderLeaflet({
              #pal &lt;- colorNumeric(&quot;YlOrRd&quot;, domain=c(min(quakes$mag), max(quakes$mag)))
              qMap &lt;- leaflet(data = (Massmpg)) %&gt;% 
                  addTiles() %&gt;%
                  addCircles(radius =3, color=&quot;red&quot;)
              qMap
          })     
          
      }
  
  
  shinyApp(ui = ui, server = server)
  </code></pre>
  <p>Matt thak you for your answer on reactive object.  I am trying it but not getting it to work. Here is my code with changes below.  I have links to the shinyapps page <a href="https://michaelcardio2020.shinyapps.io/TestFrame2/" rel="nofollow noreferrer">https://michaelcardio2020.shinyapps.io/TestFrame2/</a> and the Massmpg data in Box <a href="https://app.box.com/s/ryowy2x0h2owm3b1s18ok3342p5gharu" rel="nofollow noreferrer">https://app.box.com/s/ryowy2x0h2owm3b1s18ok3342p5gharu</a></p>
  <pre><code>library(shiny)
  library(DT)
  library(leaflet)
  Massmpg &lt;- (read.csv(&quot;Massmpg.csv&quot;))
  
  
  # Define UI for application that draws a histogram
  ui &lt;- fluidPage(titlePanel(&quot;Mass mpg by location&quot;),
                  
                  # Create a new Row in the UI for selectInputs
                  fluidRow(
                      column(4,
                             selectInput(&quot;City&quot;,
                                         &quot;City:&quot;,
                                         c(&quot;All&quot;,
                                           unique(as.character(Massmpg$City))))
                      ),
                      column(4,
                             selectInput(&quot;cyl&quot;,
                                         &quot;cyl:&quot;,
                                         c(&quot;All&quot;,
                                           unique(as.character(Massmpg$cyl))))
                      ),
                      column(4,
                             selectInput(&quot;trans&quot;,
                                         &quot;trans:&quot;,
                                         c(&quot;All&quot;,
                                           unique(as.character(Massmpg$trans))))
                      )
                  ),
                  # Create a new row for the table.
                  leafletOutput(&quot;map01&quot;),
                  DT::dataTableOutput(&quot;table&quot;)
                  
  )
  
  
      
  server &lt;- function(input, output) {
      
      data &lt;- reactive({
          x &lt;- Massmpg
      }) 
          
          # Filter data based on selections
          output$table &lt;- DT::renderDataTable(DT::datatable({
              data &lt;- Massmpg
              if (input$City != &quot;All&quot;) {
                  data &lt;- data[data$City == input$City,]
              }
              if (input$cyl != &quot;All&quot;) {
                  data &lt;- data[data$cyl == input$cyl,]
              }
              if (input$trans != &quot;All&quot;) {
                  data &lt;- data[data$trans == input$trans,]
              }
              data
          }))
          
          # map
          output$map01 &lt;- renderLeaflet({
              Massmpg &lt;- data()
              #pal &lt;- colorNumeric(&quot;YlOrRd&quot;, domain=c(min(quakes$mag), max(quakes$mag)))
              qMap &lt;- leaflet(data = (Massmpg)) %&gt;% 
                  addTiles() %&gt;%
                  addCircles(radius =3, color=&quot;red&quot;)
              qMap
          })
          
      }
  
  # Run the application 
  shinyApp(ui = ui, server = server)
  </code></pre>