R闪亮复选框组以在地图上绘制数据 [英] R shiny checkboxGroup to plot data on map

查看:98
本文介绍了R闪亮复选框组以在地图上绘制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我刚接触过光泽,所以有一个问题.
我有一个简单的数据集,它在某个位置(X,Y)上具有物种(Species)的观测值(Number_Total).


I am very new to shiny, and I have a question.
I have a simple dataset with observations (Number_Total) of species (Species), in a certain location (X,Y).

我想生成一张地图,使您能够在下拉菜单中选择物种.然后,Shiny会显示您是该物种出现在地图上.

I would like to generate a map, that enables you to select the species in a dropdown menu. Shiny then shows you were the species occurs on the map.

(根据我的经验)我走得很远,但是在菜单中选择物种并没有任何作用...

I got pretty far (for my experience), but selecting species in the menu does not do anything...

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)

我知道我的代码可能很凌乱,但我还是指责我的经验=) 我没有设法立即在此处获取示例数据集,因此此处以图片的形式出现

I know my code is probably messy, but again, I blaim my experience =) I did not manage to get an example dataset in here right away, so here it comes as picture

这是以上代码的结果(数据略有不同) 在此处输入图片描述

This is the result of the above code (with slightly different data) enter image description here

推荐答案

这就是您所需要的.我认为您有足够的技巧来理解这一点,但是如果您有任何疑问,请发表评论.

Here's what you need. I think you are skilled enough to understand this but comment if you have any questions.

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闪亮复选框组以在地图上绘制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆