1

I have a shiny app, where I want to plot CircleMarkers on a leaflet map. Additionally a marker should be plotted, controlled over an overlayGroup. When the zoom level is greater than 7 the marker should be plotted otherwise not. This is done by sending code to the server and getting the index of the marker. See also here: Show layer in leaflet map in Shiny only when zoom level > 8 with LayersControl?

It works fine, but when I add the addSearchOSM plugin from leaflet.extras the CircleMarkers will not be plotted anymore when the app starts. So the observe statement will not be rendered until I change an input.

This is the code:

library(leaflet)
library(leaflet.extras)
library(shiny)

data <- data.frame(longitude = c(11.43, 11.55), latitude = c(48, 48.5), label = c("a", "b"))

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

ui <- fluidPage(
  sidebarPanel(
    selectInput("label", "label", selected = "a", choices = data$label)
  ),
  mainPanel(
    leafletOutput("map", width = "100%", height = "700"),
    tags$head(tags$script(HTML(getInputwithJS)))
  )
)

server <- function(input, output, session){

  # subset data according to label input
  data_subset <- reactive({
    data[data$label %in% input$label, ]
  })

  output$map <- renderLeaflet({
    leaflet() %>% addTiles() %>% setView(11, 48.5, 7) %>% 
      addLayersControl(overlayGroups = c("marker1"),
                       options = layersControlOptions(collapsed = FALSE)) %>%
      addSearchOSM()
  })

  # does not show points when app starts
  observe({
      leafletProxy("map") %>% clearGroup("points") %>%
        addCircleMarkers(data_subset()$longitude, data_subset()$latitude, group = "points")
    })

  global <- reactiveValues(DOMRdy = FALSE)
  autoInvalidate <- reactiveTimer(1000)

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

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

  # add marker if marker is clicked in layerscontrol and zoom level of map > 7
  observe({
    if (!is.null(input$marker1)){
      if (input$marker1 == TRUE){
        if (input$map_zoom > 7) {
          leafletProxy("map") %>% addMarkers(lng = 11.2, lat = 48, group = "marker1")
        }else{
          leafletProxy("map") %>% clearGroup(group = "marker1")
        }
      }
    }
  })
}

shinyApp(ui, server)
Community
  • 1
  • 1
needRhelp
  • 2,948
  • 2
  • 24
  • 48
  • I cant reproduce the error. When I c&p the code and run it, it shows me a circle marker south west of Munich. – Tonio Liebrand Feb 10 '17 at 10:18
  • It works in Chrome or Microsoft Edge, but not in Firefox. – needRhelp Feb 10 '17 at 12:02
  • Firefox 51.0.1 works for me. Maybe an update helps? Otherwise I cant help with browser specifics :/ Good luck! – Tonio Liebrand Feb 10 '17 at 13:34
  • Thx. Strange, I'm using Firefox 51.0.1 too and it doesn't work, even tested this on two different PCs (Windows 10). Any idea, what could be the reason? I use R version 3.3.2, Shiny 1.0.0, Leaflet 1.0.2.9010 and leaflet.extras 0.1.9008. What are you using? – needRhelp Feb 10 '17 at 13:59
  • R 3.3.2 - Package leaflet version 1.0.2.9010; Package leaflet.extras version 0.1.9007; Package shiny version 0.14.2; RStudio Version 1.0.136. Did you check the Javascript Console in your browser for Errors? (F12, Console, JS) – Tonio Liebrand Feb 10 '17 at 14:35
  • In the console I get the error: Couldn't find map with id map – needRhelp Feb 10 '17 at 14:50
  • I have this same problem. I've tried using `observe` and `observeEvent` to no avail.... – nate Aug 22 '18 at 18:35

0 Answers0