How to get Leaflet for R use 100% of Shiny dashboard height

后端 未结 6 634
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 01:44

I am in the process of creating a Shiny dashboard application, where the dashboard body is supposed to show some maps. So far no problem to get the map expand over the entir

6条回答
  •  我在风中等你
    2020-12-13 02:14

    I personally found, that setting the height relative to window-size is more satisfying. Height as percentage does not work, because the dashboardBody has undefined height. But relative to the whole document is okay.

    100% of the dashoboardBody makes 100vh (ccs3-unit) minus header (minimum 50px) minus dashboardBody padding (2* 15px).

    So, set the height to 100vh - 80px and you should be fine.

    Since shiny does not support css3-units, this has to be included directly to the document, like in the code below.

    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)
    

    Have fun!

提交回复
热议问题