2

I'm having trouble making my leaflet map in Shiny go fullscreen. While I can obviously just manually set the width and height of the map to my screen-size, I'd like to the map to adapt to whatever screen-size it is shown on.

How do I go about automating the size of the map?

Here's the code I've been trying so far. Unfortunately, however, this doesn't adjust the height properly.

library(shiny)
library(leaflet)

location=c(46.52433,10.12633)

ui <- fluidPage(
  tags$head(tags$style(HTML("#map {height:100%, width:100%;}"))),

  leafletOutput("map")


)

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)



Any help would be greatly appreciated, thanks!

3 Answers 3

4

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)
5
  • This doesn't work -- that just makes a blank page appear. I don't think actually setting the height in the leafletOutput() statement to anything other than a pixel-specific height works, thats why I think a CSS workaround might be required.
    – seabass20
    Commented Apr 21, 2020 at 11:12
  • Try it with type = "text/css", "html, body {width:100%; height:100%}" inside tags$style()
    – monarque13
    Commented Apr 21, 2020 at 11:16
  • Thanks for being so responsive @monarque13. Unfortunatley, that didn't work either. Just so that we're on the same page, this is what I have as my tags() statement: ``` tags$head(tags$style(type = "text/css", "html, body {width:100%; height:100%}")), ``` There is no reference to the map element (though I've tried that using #map after body, and it doesn't either). Any other ideas?
    – seabass20
    Commented Apr 21, 2020 at 11:31
  • Glad to hear. Please consider upvoting/accepting my answer if you like.
    – monarque13
    Commented Apr 21, 2020 at 15:13
  • Had just been waiting to see if there was any difference between the two answers, but I've marked yours as the answer now.
    – seabass20
    Commented Apr 21, 2020 at 16:02
1

With JavaScript:

js <- '
$(document).on("shiny:connected", function(){
  $("#map").css({
    width: window.innerWidth, 
    height: window.innerHeight
  });
  $(window).on("resize", function(e){
    if(e.target instanceof Window){
      $("#map").css({width: window.innerWidth, height: window.innerHeight});
    }
  });
})
'
location=c(46.52433,10.12633)

ui <- fluidPage(
  tags$head(
    tags$style(HTML("html,body {margin: 0; overflow: hidden;}")),
    tags$script(HTML(js))
  ),
  ......
1
  • Thanks, Stephane! So both this and the edited approach from monarque13 work. Are there any differences or pros/cons to either solution?
    – seabass20
    Commented Apr 21, 2020 at 12:37
0

If you want a simple and uncomplicated solution then try view height and it shall do the thing

if(interactive()) {
  library(shiny)
  library(leaflet)
  
  location = c(46.52433, 10.12633)
  
  ui <- fluidPage(leafletOutput("map", height = "100vh"))
  
  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)
}

This solution will work fine if you want to put it within an element also.

Keep Coding!

Not the answer you're looking for? Browse other questions tagged or ask your own question.