How to Get Leaflet for R Use 100% of Shiny Dashboard Height

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

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!

leaflet in R-shiny: 100% height of leaflet map

As pointed out by Dogan Askan (thanks!) in this comment a solution using calc() and the window hight worked for me. See this answer for a more elaborated example.

Making Leaflet Map fullscreen in RShiny

Try adding width = "100%", height = "100%" to leafletOutput and using fillPage() like below:

library(shiny)
library(leaflet)

location=c(46.52433,10.12633)

ui <- fillPage(
tags$style(type = "text/css", "html, body {width:100%; height:100%}"),
leafletOutput("map", width = "100%", height = "100%")

)

server <- function(input, output, session) {

output$map = renderLeaflet({
leaflet() %>% addTiles() %>% setView(lat = location[1],lng =
location[2],zoom = 10) %>%
addMarkers(lat = location[1], lng = location[2],popup = "Test") })

}

shinyApp(ui, server)

Control the size of popupGraph() from leaflet in r shiny

popupOptions = popupOptions(maxWidth = 1000)

this piece of code is the key to solve the pb, here is the full code :

library(tidyverse)
library(ggplot2)
library(shiny)
library(leaflet)
library(leafpop)

id <- c(1,1,1,1,2,2,3,3,3,4)
lat <- c(49.823, 49.823, 49.823, 49.823, 58.478, 58.478, 57.478 , 57.478 , 57.478, 38.551)
lng <- c(-10.854, -10.854, -10.854, -10.854, -11.655, -11.655, 2.021 , 2.021 , 2.021, 5.256)
type <- c("A","C","B","B","C","A","B","A","C","B")
date <- c(152.5,307.5,145,481,152,109.5,258.5,107.5,186.5,150)
start <- c(123,235,135,192,149,101,205,75,155,100)
stop <- c(182,380,155,289,155,218,312,140,218,200)
myData <- data.frame(id,type,date,start,stop,lat,lng)

chronogramme<- function(dataId){

dataFiltered<-filter(myData,id==dataId)

p<- ggplot(dataFiltered,aes(type,date))+
geom_linerange(aes(ymin=start,ymax=stop),size=5)+
coord_flip()
p
return(p)
}

q = lapply(1:length(unique(myData$id)), function(i) {
chronogramme(i)
})

ui <- fluidPage(
leafletOutput("map", height = "100vh")
)

server <- function(input, output, session) {

#Sortie map
output$map <- renderLeaflet({
leaflet()%>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircleMarkers(
data = myData,
lat = myData$lat,
lng = myData$lng,
radius = 5,
color = 'blue',
stroke = FALSE,
fillOpacity = 1,
popup = popupGraph(q, width = 400, height = 300),
popupOptions = popupOptions(maxWidth = 1000)
)
})

}

# Create Shiny app ----
shinyApp(ui = ui, server = server)

Making a leaflet map fullscreen in an RShiny dashboard

if the point is to maximize the map over the page up to the navbar and to the left and right and down to the bottom, try the following, it may be too quick and dirty for you though.

It disables the padding/margin of the Shiny components throught CSS:

library(leaflet)
library(shiny)

# UI
ui <- navbarPage("Dashboard",
tabPanel("Fullscreen Map",
fillPage(
leafletOutput("map", width = "100vw", height = "100vh"))
),
header=tags$style(HTML("
.container-fluid{
padding: 0px !important;
}
.navbar{
margin-bottom: 0px !important;
}"))
)

# FUNCTION
server <- function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(lat = 0, lng = 0, zoom = 5)
})
}

# RUN APP
shinyApp(ui = ui, server = server)

Edit: The JavaScript version

library(leaflet)
library(shiny)

# UI
resizeJS <- "
function resizeMap(e){
$('#map').css({top: $('.navbar').height() });
}

$(window).on('resize', resizeMap);
$(window).ready(resizeMap);
"

ui <- navbarPage("Dashboard",
tabPanel("Fullscreen Map",
fillPage(
leafletOutput("map", width = "auto", height = "auto"))
),
header=tags$head( tags$style(HTML("
#map{
border: 3px solid red; /* display border around the map */
position: fixed;
left: 0px;
right: 0px;
bottom: 0px;
top: 0px;
}
.navbar{
margin-bottom: 0px !important;
}")),
tags$script(resizeJS))
)

# FUNCTION
server <- function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(lat = 0, lng = 0, zoom = 5)
})
}

# RUN APP
shinyApp(ui = ui, server = server)

This should keep the map aligned perfectly into the window.

Create an adjustable R leaflet with dashboard

There's a similar question at this link.

A solution for your flexdashboard would be to replace your column2 code with:

```{r, echo=FALSE}
output$mymap = renderLeaflet({
my_size <- input$bubblesize
leaflet() %>%
addTiles() %>%
addCircleMarkers(lng = ~long, lat = ~latt,
popup = ~planttext,
radius = ~qty * my_size, data = qty_d)
})

leafletOutput('mymap', height=1000)
```


Related Topics



Leave a reply



Submit