Home > OS >  Shiny leaflet map reactive
Shiny leaflet map reactive

Time:01-12

I am in the process of making a shiny app. I am trying to make my map interactive where the map shows only the selected Sites. Although, right now my map is showing the location of every single site in the data. This is what I have tried doing so far.( this is a simplified code)

Site_Name <-sample(c('a','b','c'),replace=T,5)
Latitude <-runif(5,min=-26, max=-22)
Longitude<-runif(5,min=-54, max=-48)
Sites <-data.frame(Site_Name,Latitude,Longitude)



fluidPage(
  theme = shinytheme("cerulean"),
  sidebarLayout(
    sidebarPanel(
      selectizeInput("sites",
                     "Site Name",choices= Sites$Site_Name,
                     options= list(maxItems = 2)),
   

   mainPanel(
      tabsetPanel(
        tabPanel("Plots",leafletOutput("Station")
   )
  )

shinyServer(function(input, output, session) {

df1 <- eventReactive(input$sites, {
    Sites %>% dplyr::filter(Site_Name %in% input$sites)
  })
  
  output$Station = renderLeaflet({
    leaflet(data = df1()) %>%
      addProviderTiles(providers$Esri.WorldStreetMap) %>%
      addMarkers(Sites$Longitude, Sites$Latitude, popup= input$sites,
                 icon = list(
                   iconUrl = 'https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-red.png',
                   iconSize = c(13, 20)))
  })
}

CodePudding user response:

It's showing all because you told to show all. You should replace Sites$Longitude, Sites$Latitude, popup= input$sites in addMarkers with lng = ~Longitude, lat = ~Latitude, popup= ~Site_Name.

  • Related