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){
if (input$map_zoom > 8) {
leafletProxy("map") %>% addMarkers(lng = 10.5, lat = 50, group = "marker")
}else{
leafletProxy("map") %>% clearMarkers()
}
}
}
})
}
shinyApp(ui = ui, server = server)