Search code examples
rshinyr-leaflet

R Shiny Leaflet Map Circles After Input Changed


I have a Shiny App that inserts a circle on a map based on the lat lng associated with the zip code input. The map renders when I load it; however, when I attempt to change the value of the zip code via a selectInput object, the map renders a blank window - i.e. the selectedZip variable.

library(shiny)
library(leaflet)

# Data
data <- read.csv('VENDOR_PERFORMANCE_EX.csv')

ui <- fluidPage(
  titlePanel("VPD"),
  sidebarLayout(
    sidebarPanel("Inputs"),
    mainPanel("Results")),
  selectInput("zipInput", "Select Zip Code", data$Zip),
  selectInput("vendorInput", "Select Vendor", as.character(data$Vendor)),
  leafletOutput("CLEmap", width = "75%", height = 600)
)

server <- function(input, output, session) {
    selectedZip <- reactive({
      data[data$Zip == input$zipInput, ]
    })
    output$CLEmap <- renderLeaflet({
      leaflet() %>% 
      addTiles() %>% 
      setView(-81.730844, 41.430102, zoom = 11) %>% 
      addCircles(data = selectedZip(), lng = ~ Y, lat = ~ X, radius = 1069)
  })
}

shinyApp(ui=ui, server = server)

Solution

  • This works, although there is something very strange going on. And although I can't be sure it fixes the same problem you have because I don't have your data, it seems likely.

    Once I added data and got something that sounded like your error I hunted around a bit. The only change I made in the end is adding a unique statement to your zipInput instance of selectInput, I was clued by the fact that that selectInput was not initializing correctly, although it was actually working other than the initial value being blank.

    I think that the selectInput control was not correctly able to deal with duplicate entries in the choices vector, and was causing the shiny control to behave strange in some way, and thereby corrupting ... something. Not really sure what.

    Weird. And not sure of what was really going on. Anyway this works. And if you take out the unique it does not work and gets an error like you describe.

    The code:

    library(shiny)
    library(leaflet)
    
    # Data
    #data <- read.csv('VENDOR_PERFORMANCE_EX.csv')
    
    data <- data.frame(Zip=c("44102","44102","44109"),
                       Vendor=c("Vendor1","Vendor2","vendor3"),
                       X=c(41.475,41.477,41.467),Y=c(-81.742,-81.748,-81.697))
    
    ui <- fluidPage(
      titlePanel("VPD"),
      sidebarLayout(
        sidebarPanel("Inputs"),
        mainPanel("Results")),
      selectInput("vendorInput", "Select Vendor", as.character(data$Vendor)),
      selectInput("zipInput", "Select Zip Code",  unique(as.character(data$Zip)) ),
      leafletOutput("CLEmap", width = "75%", height = 600)
    )
    
    server <- function(input, output, session) {
      selectedZips <- reactive({
       data[data$Zip == input$zipInput, ]
      })
      output$CLEmap <- renderLeaflet({
        leaflet() %>% 
          addTiles() %>% 
          setView(-81.730844, 41.430102, zoom = 11) %>% 
          addCircles(data=selectedZips(),lng = ~Y, lat = ~X,radius = 300 ) 
      })
    }
    
    shinyApp(ui=ui, server = server)
    

    The output:

    enter image description here