leafletProxy在modalDialog中未更新。

huangapple go评论72阅读模式
英文:

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, &quot;mymap&quot;,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 &lt;- fluidPage(actionButton(&quot;open_modal&quot;, &quot;Open Modal&quot;))


server &lt;- function(input, output) {
  
  myModal &lt;- modalDialog(
    
    radioGroupButtons(inputId = &quot;type&quot;,
                      label = &quot;Choose type of graph&quot;, 
                      choices = c(&quot;Without markers&quot;, &quot;With markers&quot;)),
    leafletOutput(&quot;mymap&quot;),
    easyClose = T
  )
  
  output$mymap &lt;- renderLeaflet(
    leaflet(data = quakes[1:20,]) %&gt;% setView(lat = -16.7, lng = 171.8, zoom = 4) %&gt;% addTiles()
  )
  
  #outputOptions(output, &quot;mymap&quot;,suspendWhenHidden=FALSE)
  
  observeEvent(input$type, {
    if(input$type == &quot;With markers&quot;) {
      leafletProxy(&quot;mymap&quot;, data = quakes[1:20,]) %&gt;% clearMarkers() %&gt;% addMarkers(~long, ~lat, popup = ~as.character(mag), label = ~as.character(mag))
    } else {
      leafletProxy(&quot;mymap&quot;, data = quakes[1:20,]) %&gt;% 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 &lt;- renderLeaflet({
    req(input$type)
    leaflet(data = quakes[1:20, ]) %&gt;% setView(lat = -16.7,
                                               lng = 171.8,
                                               zoom = 4) %&gt;% addTiles()
})

huangapple
  • 本文由 发表于 2023年7月31日 21:34:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定