最近,在处理闪亮的仪表板时,我遇到了 Leaflet 地图无法(看似)正确使用 bs4Dash 框的问题。似乎在盒子最大化后,传单地图没有更新以反映新的容器宽度。
我认为这是一个与此类似的问题:rstudio/leaflet#248。尝试了 Joe Cheng 提供的解决方案,但无法使其工作(不幸的是我不擅长 Javascript)。有没有任何解决方法可以让地图在最大化框后呈现全屏?
可重现的示例:
library(shiny)
library(bs4Dash)
library(leaflet)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
box(
title = "Map",
collapsible = TRUE,
height = "80vh",
width = 6,
maximizable = TRUE,
leafletOutput("map1", height = "100%")
)
)
)
server <- function(input, output, session) {
output$map1 <- renderLeaflet({
leaflet() %>%
setView(19.08, 60.25, zoom = 4) %>%
addTiles()
})
}
shinyApp(ui, server)
您可以为框定义一个
id
,然后在服务器端定义 updateBox
来更改宽度。试试这个
library(shiny)
library(bs4Dash)
library(leaflet)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
bs4Dash::box(
id = "mybox",
title = "Map",
collapsible = TRUE,
height = "80vh",
width = 6,
maximizable = TRUE,
leafletOutput("map1", height = "100%")
)
)
)
server <- function(input, output, session) {
output$map1 <- renderLeaflet({
leaflet() %>%
setView(19.08, 60.25, zoom = 4) %>%
addTiles()
})
observeEvent(input$mybox$maximized, {
if (input$mybox$maximized){
mywidth = 12
} else mywidth = 6
updateBox(
"mybox",
action = "update",
options = list(
width = mywidth
)
)
})
}
shinyApp(ui, server)