title: Leaflet in R plotting icons unpredictably
body: <p>My Shiny app takes a dataframe like this:</p>
  
  <p><a href="https://i.stack.imgur.com/Lsa4C.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Lsa4C.png" alt="enter image description here"></a></p>
  
  <p>and subsets appropriately by allowing the user to select a person (<code>P1_name</code>) and a date (<code>date</code>).</p>
  
  <p>When initally launched, it looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/Bp8tL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Bp8tL.png" alt="enter image description here"></a></p>
  
  <p>and already, it is clear that the app isn't working. There should be a letter 'N' at the location of the town of Apple Valley, but instead there is nothing. I can't figure out why, since the DF has been subsetted correctly:</p>
  
  <p><a href="https://i.stack.imgur.com/Hxdg9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Hxdg9.png" alt="enter image description here"></a></p>
  
  <p>and the layers should be plotted correctly:</p>
  
  <pre><code>m &lt;- leaflet(DF) %&gt;%
    addTiles() %&gt;%  # Add default OpenStreetMap map tiles
    setView(lat=setzoom[1], lng=setzoom[2], zoom=zoom_num) %&gt;%
    addMarkers(lat=subset(DF, P1_outcome=='W')$lat, lng=subset(DF, P1_outcome=='W')$lon, icon = icon_W) %&gt;%
    addMarkers(lat=subset(DF, P1_outcome=='L')$lat, lng=subset(DF, P1_outcome=='L')$lon, icon = icon_L) %&gt;%
    addMarkers(lat=subset(DF, P1_outcome=='D')$lat, lng=subset(DF, P1_outcome=='D')$lon, icon = icon_D) %&gt;%
    addMarkers(lat=subset(DF, P1_outcome=='N')$lat, lng=subset(DF, P1_outcome=='N')$lon, icon = icon_N)
  </code></pre>
  
  <p>Unfortunately, this is just one symptom of some sort of skitzophrenic behavior that my app is displaying. If that was the only problem, I'd be rejoicing. Instead, say I select John Doe, on his first row (which should be Crecent City) </p>
  
  <p><a href="https://i.stack.imgur.com/KsrTW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KsrTW.png" alt="enter image description here"></a></p>
  
  <p>and BOOM I get:</p>
  
  <p><a href="https://i.stack.imgur.com/quefP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/quefP.png" alt="enter image description here"></a></p>
  
  <p>How in the world did Leaflet think I had given it <em>two</em> sets of coordinates to plot, and what made it think that John Doe was drowing somewhere in the Pacific Ocean.</p>
  
  <p>Nothing here makes much sense. I can't see a pattern in the chaos it is outputting. It's barely 100 lines of simple code.</p>
  
  <p>Some ideas:</p>
  
  <ul>
  <li>the <code>conditionalPanel</code> is mixing up my dataframe? I don't think so, since I can <code>View(DF)</code> and see that this part isn't the problem.</li>
  <li>the layering in the icons isn't working? Not sure how this would be a problem, as we <a href="https://stackoverflow.com/questions/31930616/multiple-addcirclemarkers-layers-using-leaflet-in-r">know</a> that this is the correct way to plot icons.</li>
  <li>I am getting an <code>xtable</code> warning, <code>Warning in run(timeoutMs) : data length exceeds size of matrix</code>, but this is just for the <code>tableOutput</code> part, which I don't think is related to any of the issue I'm beseiged with.</li>
  </ul>
  
  <p>I'm stumped. Been stuck on this all day. If anyone has any insight, ideas, incantations, etc, I'd love to hear them.</p>
  
  <p><strong>UI.R</strong></p>
  
  <pre><code>library(shiny)
  library(ggplot2)
  library(dplyr)
  library(leaflet)
  library(data.table)
  options(xtable.include.rownames=F)
  library(ggmap)
  library(lubridate)
  
  DF &lt;- data.frame(lon=c(-120.6596156, -87.27751, -119.7725868, -124.2026, -117.1858759),  
                   lat=c(35.2827524, 33.83122, 36.7468422, 41.75575, 34.5008311), 
                   date=c('2014-03-14', '2014-01-11', '2013-11-22', '2012-08-23', '2013-08-23'),
                   location=c('San Luis Obispo', 'Jasper', 'Fresno', 'Crescent City', 'Apple Valley'), 
                   P1_name=c('John Doe', 'John Doe', 'John Doe', 'John Doe', 'Joe Blow'),
                   P1_outcome=c('W', 'L', 'D', 'W', 'N'))
  
  DF$date &lt;- as.Date(DF$date, format="%Y-%m-%d")
  DF &lt;- arrange(DF, P1_name, date)
  DT &lt;- data.table(DF)
  DT[, .date := sequence(.N), by = "P1_name"]
  DF$date &lt;- paste(DF$date, '   (', DT$.date, ')', sep='')
  DF &lt;- arrange(DF, P1_name, desc(date))
  DF$P1_name &lt;- as.character(DF$P1_name)
  DF$P1_outcome &lt;- as.character(DF$P1_outcome)
  DF$location &lt;- as.character(DF$P1_location)
  #str(DF$P1_outcome)
  
  icon_W &lt;- makeIcon(
    iconUrl = "http://i58.tinypic.com/119m3r5_th.gif",
    iconWidth = 10, iconHeight = 23,
    iconAnchorX = 10, iconAnchorY =23 
  )
  
  icon_L &lt;- makeIcon(
    iconUrl = "http://i62.tinypic.com/2dulcvq_th.jpg",
    iconWidth = 10, iconHeight = 23,
    iconAnchorX = 10, iconAnchorY = 23
  )
  
  icon_D &lt;- makeIcon(
    iconUrl = "http://i58.tinypic.com/2zox2yf_th.gif",
    iconWidth = 10, iconHeight = 23,
    iconAnchorX = 10, iconAnchorY = 23
  )
  
  icon_N &lt;- makeIcon(
    iconUrl = "http://i62.tinypic.com/339j7de_th.gif",
    iconWidth = 10, iconHeight = 23,
    iconAnchorX = 22, iconAnchorY = 94
  )
  
  server &lt;- function(input, output, session) {
  
    output$dates&lt;-renderUI({
      selectInput('dates', 'by date / number', choices=DF[which(DF$P1_name == input$person), ]$date, selectize = FALSE)
    })
  
    output$map&lt;-renderLeaflet({
      validate(
        need(!is.null(input$dates),""),
        need(!is.null(input$person),"")
      )
  
      if(input$radio=='by date'){
        DF &lt;- filter(DF, P1_name==input$person, date==input$dates)
        View(DF)   
        zoom_num &lt;- 5
        setzoom &lt;- c(DF$lat, DF$lon) 
        outcome &lt;- data.frame(DF$P1_outcome, DF$location)
        output$table &lt;- renderTable(outcome)
      }
      else{
        DF &lt;- filter(DF, P1_name==input$person)
        View(DF)
        zoom_num &lt;- 2
        setzoom &lt;- c(DF$lat[1], DF$lon[1])
        outcome &lt;- data.frame(DF$P1_outcome, DF$location)
        output$table &lt;- renderTable(outcome)
      }
  
  
  
      m &lt;- leaflet(DF) %&gt;%
        addTiles() %&gt;%  # Add default OpenStreetMap map tiles
        setView(lat=setzoom[1], lng=setzoom[2], zoom=zoom_num) %&gt;%
        addMarkers(lat=subset(DF, P1_outcome=='W')$lat, lng=subset(DF, P1_outcome=='W')$lon, icon = icon_W) %&gt;%
        addMarkers(lat=subset(DF, P1_outcome=='L')$lat, lng=subset(DF, P1_outcome=='L')$lon, icon = icon_L) %&gt;%
        addMarkers(lat=subset(DF, P1_outcome=='D')$lat, lng=subset(DF, P1_outcome=='D')$lon, icon = icon_D) %&gt;%
        addMarkers(lat=subset(DF, P1_outcome=='N')$lat, lng=subset(DF, P1_outcome=='N')$lon, icon = icon_N)
    })  #&lt;- end output$map
  }     #&lt;- end server function
  
  ui &lt;- fluidPage(
    titlePanel("Location Explorer"),
    sidebarLayout (
      sidebarPanel(
        selectInput('person', 'Select person', choices=unique(DF$P1_name), selectize = FALSE),
        radioButtons('radio', 'Select row(s)', choices=c('by date', 'all'), selected = NULL, inline = TRUE),
        conditionalPanel(
          condition = "input.radio == 'by date'",
          uiOutput('dates')   
        ),
        conditionalPanel(
          condition = "input.radio == 'all'"
        )      
      ),
      mainPanel(
        leafletOutput('map'),      
        fluidRow(column(4, tableOutput('table')))
      ))
  )  #&lt;-  end ui
  
  shinyApp(ui = ui, server = server)
  </code></pre>