I am using leaflet and shinydashboard to create shiny app which have multiple menus which have a problem of leaftletProxy
.
Here I created minimum example to show my question (https://gist.github.com/byzheng/074c3c1ff75ea9f951f5).
In the app, there are two sidebar menus 1) the first menu has a link click me
; 2) the second menu has a leaflet map. After clicking click me
in the first page, the second page is enabled and then setView
of leaflet map to a random place.
The problem is the js console has an error message Couldn't find map with id map
when the click me
is clicked in the first time and leafletProxy
is called.
I think this problem is related with leaflet map isn't initialized when shiny app is loading. After clicking more than one time, everything is working as expected.
So my question is how to force shiny to draw leaflet map when shiny app is loading.
The issue here is that the code creating the leaflet map is suspended while the output$map
is hidden.
One way to fix this could be to use:
outputOptions(output,"map",suspendWhenHidden=FALSE)
Unfortunately this seems to be buggy right now but could be fixed soon, it currently throws a js error (see here).
Since output$summary
seems to be run after the renderLeaflet
, you could use setView
in that block as a temporary solution.
output$summary <- renderPrint({
leafletProxy('map') %>% setView(runif(1) * 30 +2, runif(1) * 30 + 2, 7)
print(input$mydata)
print(leafletProxy('map')$id)
})