英文:
leafletProxy in modalDialog is not updated
问题
我有一个相当简单的应用程序,我试图在modalDialog内呈现一个leaflet地图。在这个modalDialog内,一些输入允许用户自定义地图。第一次打开modalDialog时,它运行得很好,但一旦第二次打开,似乎什么都不再响应。
我查看了其他帖子,建议使用 outputOptions(output, "mymap",suspendWhenHidden=FALSE)
,但它并没有解决问题(相反,它阻止了第一次的工作)。
以下是一个简单的示例:
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)
英文:
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)
答案1
得分: 3
你应该在renderLeaflet
内部添加一个req(input$type)
,然后它就会工作。
output$mymap <- renderLeaflet({
req(input$type)
leaflet(data = quakes[1:20, ]) %>% setView(lat = -16.7, lng = 171.8, zoom = 4) %>% addTiles()
})
英文:
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()
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论