如何获得Leaflet for R使用100%的Shiny仪表板高度

Tom*_*Geo 43 r leaflet shiny shinydashboard

我正在创建一个Shiny仪表板应用程序,其中仪表板主体应该显示一些地图.到目前为止,没有问题让地图扩展到整个身体的宽度,但它不知何故不愿意调整到整个高度. 在此输入图像描述

传单本身已经设置为覆盖100%的高度,但它不能解决问题.只要我对leafletOutput使用height属性,传单对象就不会显示,而且我留下了一个空框.

代码可以在下面找到:

library(shinydashboard)
library(leaflet)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(
    sidebarMenu(
      menuItem(
        "Maps", 
        tabName = "maps", 
        icon = icon("globe"),
        menuSubItem("Watersheds", tabName = "m_water", icon = icon("map")),
        menuSubItem("Population", tabName = "m_pop", icon = icon("map"))
      ),
      menuItem(
        "Charts", 
        tabName = "charts", 
        icon = icon("bar-chart"),
        menuSubItem("Watersheds", tabName = "c_water", icon = icon("area-chart")),
        menuSubItem("Population", tabName = "c_pop", icon = icon("area-chart"))
      )
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(
        tabName = "m_water",
        box(
          title = "Baltic catchment areas",
          collapsible = TRUE,
          width = "100%",
          height = "100%",
          leafletOutput("l_watershed")
        )
      ),
      tabItem(
        tabName = "m_pop",
        # Map in Dashboard
        leafletOutput("l_population")
      ),
      tabItem(
        tabName = "charts",
        h2("Second tab content")
      )
    )
  )
)

server <- function(input, output) {
  set.seed(122)
  histdata <- rnorm(500)

  output$l_watershed <- renderLeaflet({
    leaflet(height = "100%") %>% addTiles() %>% setView(19.08, 60.25, zoom = 4) %>%addWMSTiles(
      "http://62.236.121.188/arcgis/services/DataAndMaps/Background/MapServer/WMSServer?",
      layers = "11",
      options = WMSTileOptions(
        format = "image/png",
        transparent = TRUE
      ),
      attribution = "Catchment area provided by HELCOM"
    )
  })

  output$l_population <- renderLeaflet({
    leaflet(height = "100%") %>% addTiles() %>% setView(19.08, 60.25, zoom = 4) %>%addWMSTiles(
      "http://62.236.121.188/arcgis/services/DataAndMaps/Background/MapServer/WMSServer?",
      layers = "17",
      options = WMSTileOptions(
        format = "image/png",
        transparent = TRUE
      ),
      attribution = "Population data provided by HELCOM"
    )
  })
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

K. *_*hde 62

我个人发现,相对于窗口大小设置高度更令人满意.高度百分比不起作用,因为dashboardBody具有未定义的高度.但相对于整个文件是可以的.

100%的dashoboardBody使100vh(ccs3-unit)减去标题(最小50px)减去dashboardBody padding(2*15px).

所以,将高度设置为100vh - 80px,你应该没问题.

由于有光泽不支持css3单元,因此必须将其直接包含在文档中,如下面的代码所示.

library(shiny)
library(shinydashboard)
library(leaflet)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
    leafletOutput("map")
  )
)

server <- function(input, output) {
  output$map <- renderLeaflet({
    leaflet() %>% addTiles() %>% setView(42, 16, 4)
  })
}

runApp(shinyApp(ui, server), launch.browser = TRUE)
Run Code Online (Sandbox Code Playgroud)

玩得开心!

  • 您还可以在输出中直接使用视口高度(vh):leafletOutput("map",height ="95vh") (11认同)
  • 很爱很爱你 !我折磨了一个月以找到解决方法 (3认同)

小智 7

另一种选择是jcheng5kent37GitHub 上所描述的

output$mymap = renderLeaflet({...make a map...})
leafletOutput('mymap', height=1000)
Run Code Online (Sandbox Code Playgroud)

对我的作品有leaflet mapR flexdashboard


Por*_*hop 6

尝试手动添加像素大小:

...
  dashboardBody(
    tabItems(
      tabItem(
        tabName = "m_water",
        box(
          title = "Baltic catchment areas",
          collapsible = TRUE,
          width = "100%",
          height = "1000px",
          leafletOutput("l_watershed",width="100%",height="1000px")
        )
      ),
      tabItem(
        tabName = "m_pop",
        # Map in Dashboard
        leafletOutput("l_population",width="100%",height="1000px")
      ),
      tabItem(
        tabName = "charts",
        h2("Second tab content")
      )
    )
  )
...
Run Code Online (Sandbox Code Playgroud)

  • 或者在 `Box` 中,您可以使用 `tags$style(type = "text/css", ".box-body {height:80vh}"),`,然后使用 `leafletOutput("l_watershed",width="100%" ,高度=“100%”)` (2认同)