-->

ř光泽checkboxGroup绘制在地图数据上(R shiny checkboxGroup to

2019-11-05 07:25发布


我很新的光泽,而且我有一个问题。
我有意见的物种(NUMBER_TOTAL)(种)一个简单的数据集,在某个位置(X,Y)。

我想生成的地图,使您可以选择在下拉菜单中的物种。 闪亮则说明你在地图上出现的品种。

我得到了很远(我的经验),但在菜单中选择种什么都不做......

ui <- (fluidPage(titlePanel("Species Checker"),  
                 sidebarLayout(
                   sidebarPanel(
                      selectizeInput('species', 'Choose species', 
                   choices    = df$Species, multiple = TRUE)
                     ),
                   mainPanel(
                     leafletOutput("CountryMap", 
               width = 1000, height = 500))
                 )
))

服务器端

server <- function(input, output, session){
  output$CountryMap <- renderLeaflet({
    leaflet() %>% addTiles() %>% 
      setView(lng = 10, lat = 40, zoom = 5) %>%
      addCircles(lng = df$Y, lat = df$X, weight = 10, 
      radius =sqrt(df$Number_Total)*15000, popup = df$Species)
  })


  observeEvent(input$species, {

    if(input$species != "")
    {
      leafletProxy("CountryMap") %>% clearShapes()
      index = which(df$Species == input$species)
      leafletProxy("CountryMap")%>% addCircles(lng = df$X[index], 
      lat = df$Y[index],
                                               weight = 1, 
     radius =sqrt(df$Number_Total[index])*30, popup = df$Species[index])
    }
  }) 

}

最后绘制它

shinyApp(ui = ui, server = server)

我知道我的代码可能是混乱的,但同样,我blaim我的经验=)我没能拿到这里马上一个例子的数据集,所以在这里它作为图片

这是上面的代码的结果(具有略微不同的数据) 在这里输入图像描述

Answer 1:

下面是你需要的。 我觉得你不够熟练,了解这一点,但评论,如果您有任何问题。

server <- function(input, output, session) {
  # map_data <- reactive({
    # req(input$species)
    # df[df$Species %in% input$species, ]
  # })

  output$CountryMap <- renderLeaflet({
    leaflet() %>% addTiles() %>% 
      setView(lng = 10, lat = 40, zoom = 5)
  })

  map_proxy <- leafletProxy("CountryMap")

  observe({
    md <- df[df$Species %in% input$species, ]
    map_proxy %>%
      addCircles(lng = md$Y, lat = md$X, weight = 10, 
      radius = sqrt(md$Number_Total)*15000, popup = md$Species)
  })
}


文章来源: R shiny checkboxGroup to plot data on map