简体   繁体   English

Shiny leaflet map 无功

[英]Shiny leaflet map reactive

I am in the process of making a shiny app.我正在制作一个 shiny 应用程序。 I am trying to make my map interactive where the map shows only the selected Sites.我正在尝试使我的 map 具有交互性,其中 map 仅显示选定的站点。 Although, right now my map is showing the location of every single site in the data.虽然,现在我的 map 正在显示数据中每个站点的位置。 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)))
  })
}

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 .您应该将addMarkers中的Sites$Longitude, Sites$Latitude, popup= input$sites替换为lng = ~Longitude, lat = ~Latitude, popup= ~Site_Name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM