R Shiny的Leaflet地图与checkboxGroupInput一起使用时,每次只显示两个标记点。

huangapple go评论106阅读模式
英文:

R Shiny Leaflet map with checkboxGroupInput only displays two markers at a time

问题

我正在尝试编写一个应用程序,允许用户通过checkboxGroupInput选择多个点组,并通过leaflet地图显示相应的带有经纬度的点。我的数据框如下所示:

x3 = structure(list(
     species = c("Duck", "Duck", "Goose", "Goose", "Swan", "Swan"), 
     lat = c(42, 43, 47, 38, 40, 40), 
     long = c(-110, -88, -122, -77, -112, -109)), 
     row.names = c(NA, 6L), class = "data.frame")

使用这个数据框,我期望每个物种显示两个点,如果勾选了所有三个物种的框,则最多同时显示六个点。然而,我无法同时显示超过两个点。如果一次只勾选一个框,点是正确的,但是当勾选两个或更多框时,只显示两个类别中的一个点,其他点则消失。我的代码如下:

library(shiny)
library(leaflet)

#create dataframe 
x3 = structure(list(
     species = c("Duck", "Duck", "Goose", "Goose", "Swan", "Swan"), 
     lat = c(42, 43, 47, 38, 40, 40), 
     long = c(-110, -88, -122, -77, -112, -109)), 
     row.names = c(NA, 6L), class = "data.frame")
  
#setup user interface
ui = fluidPage(
  
  titlePanel("Bird Map"),
  
  sidebarLayout(
    
    sidebarPanel = sidebarPanel(
      
      checkboxGroupInput(inputId = "species1", 
                         label = "Species", 
                         choices = list("Duck",
                                        "Goose",
                                        "Swan"),
                         selected = "Duck"),
    ),
    
 #Main panel appearance    
    mainPanel = mainPanel(
      leafletOutput(outputId = 'map')
    )
  )
)

#Reactive server function
server = function(input, output){
  map_x3 = reactive({
    x3 %>%
      filter(species == input$species1) 
  })
  
  #Leaflet map output 
  output$map = renderLeaflet({
    leaflet(map_x3()) %>%
      addTiles() %>%
      setView(lng = -90, lat = 36.2, zoom = 4)%>%
      clearShapes() %>%
      addMarkers(label = ~ species)
  })
}

#Run app
shinyApp(ui = ui, server = server)

非常感谢您的帮助。我已经尝试了不同的显示数据的方法,简化和更复杂的数据集,还有observe()部分和其他反应表达式。问题仍然存在,尽管在更大的数据集中,我通常能够同时显示超过两个点(但是当勾选多个框时,所有点都不可见)。

英文:

I'm trying to code an app that allows users to select multiple groups of points via checkboxGroupInput and have a leaflet map display the associated points with corresponding lat/longs. My dataframe is as follows:

x3 = structure(list(
     species = c("Duck", "Duck", "Goose", "Goose", "Swan", "Swan"), 
     lat = c(42, 43, 47, 38, 40, 40), 
     long = c(-110, -88, -122, -77, -112, -109)), 
     row.names = c(NA, 6L), class = "data.frame")

Using this dataframe, I'd expect two points to appear for each species, and up to six points to appear at once if all three species boxes are checked. However, I've been unable to get more than two points at a time to display. If only one box at a time is checked, the points are correct, but when two or more boxes are checked, one point from each of two categories is displayed and the other points are missing. My code is as follows:

library(shiny)
library(leaflet)

#create dataframe 
x3 = structure(list(
     species = c("Duck", "Duck", "Goose", "Goose", "Swan", "Swan"), 
     lat = c(42, 43, 47, 38, 40, 40), 
     long = c(-110, -88, -122, -77, -112, -109)), 
     row.names = c(NA, 6L), class = "data.frame")
  
#setup user interface
ui = fluidPage(
  
  titlePanel("Bird Map"),
  
  sidebarLayout(
    
    sidebarPanel = sidebarPanel(
      
      checkboxGroupInput(inputId = "species1", 
                         label = "Species", 
                         choices = list("Duck",
                                        "Goose",
                                        "Swan"),
                         selected = "Duck"),
    ),
    
 #Main panel appearance    
    mainPanel = mainPanel(
      leafletOutput(outputId = 'map')
    )
  )
)

#Reactive server function
server = function(input, output){
  map_x3 = reactive({
    x3 %>%
      filter(species == input$species1) 
  })
  
  #Leaflet map output 
  output$map = renderLeaflet({
    leaflet(map_x3()) %>%
      addTiles() %>%
      setView(lng = -90, lat = 36.2, zoom = 4)%>%
      clearShapes() %>%
      addMarkers(label = ~ species)
  })
}

#Run app
shinyApp(ui = ui, server = server)

Thank you in advance, your help is appreciated.

I've experimented with different ways to display the data, simplified and more complex datasets, an observe () section, and additional reactive expressions. The problem still occurred, although with larger datasets I was often able to get more than two points to be displayed at once (although all points were never visible when multiple boxes were checked).

答案1

得分: 0

你应该将x3 %>% filter(species == input$species1)替换为

x3 %>% 
    dplyr::filter(species %in% input$species1) 

R Shiny的Leaflet地图与checkboxGroupInput一起使用时,每次只显示两个标记点。

英文:

You should replace x3 %>% filter(species == input$species1) with

x3 %>% 
    dplyr::filter(species %in% input$species1) 

R Shiny的Leaflet地图与checkboxGroupInput一起使用时,每次只显示两个标记点。

huangapple
  • 本文由 发表于 2023年8月9日 04:21:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定