ObserveEvent of group legend in Shiny 观察事件

ObserveEvent of group legend in Shiny

我正在研究 Shiny,我想用 ObserveEvent 捕获用户在以下地图的图例中单击的 group/BaseGroup:

  output$map <- renderLeaflet({
    p <- leaflet(paises_total_casos()) %>%
      addTiles() %>%
      setView( lat=10, lng=0 , zoom=2) %>%
      addCircles(lng = ~cent$x, lat = ~cent$y, weight = 1, radius = ~sqrt(total_casos) * 40, color = "blue", group = "New_cases",
                 label = ~htmlEscape(paste(location, ":", format(as.numeric(total_casos), big.mark=","), sep = " "))) %>%
      addCircles(lng = ~cent$x, lat = ~cent$y, weight = 1, radius = ~sqrt(total_fallecidos) * 40, color = "red", group = "New_deaths",
                 label = ~htmlEscape(paste(location, ":", format(as.numeric(total_fallecidos), big.mark=","), sep = " "))) %>%
      addCircles(lng = ~cent$x, lat = ~cent$y, weight = 1, radius = ~sqrt(tests) * 40, color = "green", group = "New_tests",
                 label = ~htmlEscape(paste(location, ":", format(as.numeric(tests), big.mark=","), sep = " "))) %>%
      
      #Afegim el Layers Control
      
      addLayersControl(baseGroups = c("New_cases", "New_deaths", "New_tests"),
                       options = layersControlOptions(collapsed = FALSE))
    
  })

假设我想捕捉地图显示组 New_cases、New_deaths 或 New_tests。

有没有可能用 ObserveEvent 做到这一点?

谢谢

您可以为您的地图添加观察员。您可以使用 input$map_groups(将“_groups”添加到使用的 outputId)并放置在 observe 中。请参阅下面的完整示例,该示例将打印所示的地图图层。

library(shiny)
library(leaflet)

ui <- fluidPage(leafletOutput("map"))

server <- function(input, output, session) {
  
  output$map <- renderLeaflet({
    leaflet() %>%
      addTiles(group = "OpenStreetMap") %>%
      addProviderTiles("Stamen.Toner", group = "Toner by Stamen") %>%
      addMarkers(runif(20, -75, -74), runif(20, 41, 42), group = "Markers") %>%
      addLayersControl(
        baseGroups = c("OpenStreetMap", "Toner by Stamen"),
        overlayGroups = c("Markers")
      )
  })
  
  observe({
    print(input$map_groups)
  })
  
}

shinyApp(ui, server)