Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 entire width of the body, but it's somehow not willing to adjust to the full height. enter image description here

The leaflet itself is already set to cover 100% of the height, but it doesn't do the trick. As soon as I use the height attribute for the leafletOutput the leaflet object will not show at all, and I am left with an empty box.

The code can be found below:

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) 
like image 570
TomGeo Avatar asked Apr 07 '16 07:04

TomGeo


2 Answers

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!

like image 109
K. Rohde Avatar answered Sep 18 '22 22:09

K. Rohde


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

like image 31
Banshae Avatar answered Sep 19 '22 22:09

Banshae