I have a quite simple application where I try to render a leaflet map inside a modalDialog. Inside this modalDialog, some inputs allow the user to customize the map.
It works pretty well the first time the modalDialog is opened, but as soon as it is the second time, nothing seems to be reactive anymore.
I looked into other posts which suggested to use outputOptions(output, "mymap",suspendWhenHidden=FALSE)
but it does not fix the problem (on the contrary, it prevents the first time from working).
Here a simple repex:
library(shiny)
library(leaflet)
library(shinyWidgets)
data(quakes)
ui <- fluidPage(actionButton("open_modal", "Open Modal"))
server <- function(input, output) {
myModal <- modalDialog(
radioGroupButtons(inputId = "type",
label = "Choose type of graph",
choices = c("Without markers", "With markers")),
leafletOutput("mymap"),
easyClose = T
)
output$mymap <- renderLeaflet(
leaflet(data = quakes[1:20,]) %>% setView(lat = -16.7, lng = 171.8, zoom = 4) %>% addTiles()
)
#outputOptions(output, "mymap",suspendWhenHidden=FALSE)
observeEvent(input$type, {
if(input$type == "With markers") {
leafletProxy("mymap", data = quakes[1:20,]) %>% clearMarkers() %>% addMarkers(~long, ~lat, popup = ~as.character(mag), label = ~as.character(mag))
} else {
leafletProxy("mymap", data = quakes[1:20,]) %>% clearMarkers()
}
})
observeEvent(input$open_modal, {
showModal(myModal)
})
}
shinyApp(ui = ui, server = server)
You should add a req(input$type)
inside the renderLeaflet
and it will work.
output$mymap <- renderLeaflet({
req(input$type)
leaflet(data = quakes[1:20, ]) %>% setView(lat = -16.7,
lng = 171.8,
zoom = 4) %>% addTiles()
})