Show layer on booklet map in Shiny only with zoom level> 8 using LayersControl?

I want to show the layer only when it is clicked in the LayersControl and the zoom level is greater than a certain number, for example. 8. One of the reasons is that in order to obtain the coordinates of a layer, several expensive calculations are necessary. I want to use a layercontrol rather than an extra enter button (for optical reasons).

Is there a way to get the value if the layer button is pressed in the layercontrol?

Here is a simple example (doesn't work):

library(leaflet) 
library(shiny)

ui <- fluidPage(
  leafletOutput("map", width = "100%", height = "700")
)

server <- function(input, output){
  output$map <- renderLeaflet({
    leaflet() %>% addTiles() %>% setView(10.4, 50.3, 7) %>%
      addLayersControl(overlayGroups = c("marker"),
                       options = layersControlOptions(collapsed = FALSE))
  })

  observe({
   # if (input$marker == TRUE){ # how to get value if layercontrol is clicked?
      if (input$map_zoom > 8) {
        leafletProxy("map") %>% addMarkers(lng = 10.5, lat = 50, group = "marker")
      }
  #  }
  })
}

shinyApp(ui = ui, server = server)
+3
source share
1 answer

Here is the first launched version. Maybe smdy came up with sthg "cleaner" :).

:

1: $ . ( ), "Inspect Element" . . . , , textinput sthg " ". , id,....

2: , : ( , JS R : : https://ryouready.wordpress.com/2013/11/20/sending-data-from-client-to-server-and-back-using-shiny/) : , , google. : document.getElementsByTagName("input"). (: , ) , . . console.log() javascript- ( "F12" β†’ (JS).) HtMLCollection, , .

3: HTMLCollection

(), , , JS "DOM". , script "<body></body>". , . window.onload() document.ready(). : session $onFlushed() R "JS". ( R Shiny.onInputChange("marker", inputs[0].checked);) β†’ " $". , . .

4: $marker , .onclicked()/ . , - . , , autoInvalidate().

5: , , , . , . , , , . , , , %>% clearMarkers() - .

library(leaflet)
library(shiny)

getInputwithJS <- '
Shiny.addCustomMessageHandler("findInput",
  function(message) {
  var inputs = document.getElementsByTagName("input");
  Shiny.onInputChange("marker", inputs[0].checked);
}
);
'

ui <- fluidPage(

  leafletOutput("map", width = "100%", height = "700"),
  tags$head(tags$script(HTML(getInputwithJS)))
)

server <- function(input, output, session){
  global <- reactiveValues(DOMRdy = FALSE)
  output$map <- renderLeaflet({
    leaflet() %>% addTiles() %>% setView(10.4, 50.3, 7) %>%
      addLayersControl(overlayGroups = c("marker"),
                       options = layersControlOptions(collapsed = FALSE))
  })

  autoInvalidate <- reactiveTimer(1)

  observe({
    autoInvalidate()
    if(global$DOMRdy){
      session$sendCustomMessage(type = "findInput", message = "")      
    }
  })

  session$onFlushed(function() {
    global$DOMRdy <- TRUE
  })

  observe({
    if (!is.null(input$marker)){
      if (input$marker == TRUE){ # how to get value if layercontrol is clicked?
        if (input$map_zoom > 8) {
          leafletProxy("map") %>% addMarkers(lng = 10.5, lat = 50, group = "marker")
        }else{
          leafletProxy("map") %>% clearMarkers()
        }
      }
    }
  })
}

shinyApp(ui = ui, server = server)
+4

Source: https://habr.com/ru/post/1665764/


All Articles