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

后端 未结 6 611
伪装坚强ぢ
伪装坚强ぢ 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:12

    Building on the answer of @K. Rohde I've been using a custom css file containing

    #map {
        height: calc(100vh - 130px) !important;
    }
    
    @media only screen and (min-width: 768px) {
        #map {
        height: calc(100vh - 80px) !important;
        }
    }
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-12-13 02:25

    You can also do it if you want to combine your leaflet map with another kind of panel structures like a navBarPage, including the map in a tabPanel:

    tags$style(type = "text/css", "html, body {width:100%;height:100%}"), tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"), leafletOutput("map", width = "100%", height = "100%"),

    0 讨论(0)
  • 2020-12-13 02:26

    Try adding the pixel size manually:

    ...
      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")
          )
        )
      )
    ...
    
    0 讨论(0)
  • 2020-12-13 02:28

    Th vh unit is not working for some old mobile browser. This css below should work for computer and mobile.

    /* for computer */
    div.outer {
         height: calc(100vh - 80px);
         padding: 0;
         margin: 0;
         min-height: 500px
    }
    
    @media all and (max-width:768px){
    /* for mobile */
    div.outer  {
      position: fixed;
      top: 70px;
      left: 0;
      right: 0;
      bottom: 0;
      overflow: hidden;
      padding: 0;
    }
    }
    
    0 讨论(0)
  • 2020-12-13 02:31

    Another option is what jcheng5 and kent37 have described on GitHub

    output$mymap = renderLeaflet({...make a map...})
    leafletOutput('mymap', height=1000)
    

    Works for me with a leaflet map in R flexdashboard

    0 讨论(0)
提交回复
热议问题