简体   繁体   中英

ObserveEvent of group legend in Shiny

I am working on Shiny and I would like to capture with a ObserveEvent the group/BaseGroup that the user is clicking in the legend of the following map:

  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))
    
  })

Let's say I would like to capture if the map is showing the group New_cases, New_deaths or New_tests.

Is there a possibility to do that with ObserveEvent?

Thank you

You can include an observer for your map. You can use input$map_groups (adding "_groups" to the outputId used) and place inside observe . See complete example below which will print the map layer shown.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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