title: How to calculate distance between 2 points with leaflet, geosphere, and R shiny
body: <p>This is a simplified working representation of my code.  I have two collections of locations of points. One is automobiles and the other is dealers.   There is a slider of distance in miles.  If a user clicks on the dealer location and selects the distance on the slider, only the auto locations within the distance from the dealer selected will be visible on the map.  I have a another picklist of city which works well.  However, when I pick a dealer the display goes to an error and I get <em><em>Warning: Error in .pointsToMatrix: points should be vectors of length 2, matrices with 2 columns, or inheriting from a SpatialPoints</em> object</em>* I have looked at some other questiosn with the same error bu they are very different problems.  Thanks in advance !</p>
  <pre><code>
  
  library(shiny)
  library(DT)
  library(ggplot2)
  library(dplyr)
  library(leaflet)
  library(geosphere)
  r = 3959
  
  City &lt;- c(&quot;Boston&quot;,&quot;Boston&quot;, &quot;Boston&quot;, &quot;Lowell&quot;,&quot;Lowell&quot;, &quot;Lowell&quot;,&quot;Worcestor&quot;, &quot;Worcestor&quot;,&quot;Worcestor&quot;,&quot;Springfield&quot;,&quot;Springfield&quot;,&quot;Springfield&quot;)
  lat &lt;- c(42.35, 42.355, 42.345, 42.63,42.625,42.635,42.27,42.265,42.275, 42.1,42.105,42.095)
  lng &lt;- c(-71.05,-71.045,-71.055,-71.316,-71.315,-71.317,-71.79,-71.785,-71.795,-72.6,-72.595,-72.605)
  
  MassLocations &lt;- data.frame(City, lat,lng)
  # MassLocations has 4 cities with 3 locations each
  
  
  Dealer &lt;- c(&quot;West&quot;,&quot;Central&quot;, &quot;East&quot;)
  lat &lt;- c(42.1, 42.0, 42.2)
  lng &lt;- c(-72.5,-71.8, -71.1)
  
  MassDealers &lt;- data.frame(Dealer, lat, lng)
  #massDelaers has 3 dealers in West, Central, and East
  
  
  ui &lt;- fluidPage(titlePanel(&quot;Mass mpg by location&quot;),
                  
                  # Create a new Row in the UI for selectInputs
                  fluidRow(
                      column(4,
                             selectInput(&quot;Dealer&quot;,
                                         &quot;Dealer:&quot;,
                                         c(&quot;All&quot;,
                                           unique(as.character(MassDealers$Dealer)))),
                             sliderInput(&quot;Distance&quot;, 
                                         &quot;Distance from Dealer:&quot;, 
                                         min = 1,
                                         max = 100, 
                                         value = 100),
                             selectInput(&quot;City&quot;,
                                         &quot;City:&quot;,
                                         c(&quot;All&quot;,
                                           unique(as.character(MassLocations$City))))
                      ),
                              ),
                  # Create a new row for the table.
                  leafletOutput(&quot;map01&quot;),
                  DT::dataTableOutput(&quot;table&quot;)
                  
  )
  
  
      
  
  # Define server logic required to draw a histogram
  server &lt;- function(input, output) {
      
      
              # Filter data based on selections
              dataShow &lt;- reactive({
                  data &lt;- MassLocations
                  Ddata &lt;- MassDealers
                  if (input$Dealer != &quot;All&quot;) {
                      Ddata &lt;- Ddata[Ddata$Dealer == input$Dealer]
                      data$Distance &lt;- (distHaversine((cbind(Ddata$lng,Ddata$lat)),cbind(data$lng,data$lat)))/1600
                      data &lt;- data[data$Distance &lt; input$Distance, ]
                  }
                  
                  
                  
                  
                  if (input$City != &quot;All&quot;) {
                      data &lt;- data[data$City == input$City, ]
                  }
                  data
              })
              
              # Display
              output$table &lt;- DT::renderDataTable(
                  DT::datatable(dataShow()))      
          
          
          
          
          # map
          output$map01 &lt;- renderLeaflet({
              #pal &lt;- colorNumeric(&quot;YlOrRd&quot;, domain=c(min(quakes$mag), max(quakes$mag)))
              qMap &lt;- leaflet(data = (dataShow())) %&gt;% 
                  addTiles() %&gt;%
                  addCircles(radius =3, color=&quot;red&quot;)
              qMap
          })
          
  } 
          
  # Run the application 
  shinyApp(ui = ui, server = server)
  
  
  </code></pre>