How to plot leaflet map in shiny mainPanel

How can you resize a leaflet map in the mainPanel of a shiny app? Any hints please?

The following works, but the map is too small. (EDIT: By too small, I mean that when the page is set to full the map is only rendered vertically for a small bit rather than for the full vertical page)

ui <- bootstrapPage(
            sidebarLayout(
                sidebarPanel("sidebar panel"),
                mainPanel(leafletOutput("map") 
            )))

server <- function(input, output, session) {
  output$map <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%  
      addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R") 
      })
}

shinyApp(ui, server)

Changing mainPanel(leafletOutput("map") ) to mainPanel(leafletOutput("map", width = "100%", height = "100%") ) stops the map from rendering. Similarly, the following does not render

ui <- bootstrapPage(
            sidebarLayout(
                column(3, "sidebar panel"),
                column(9, leafletOutput("map", width = "100%", height = "100%"))  # the 100% stops this running
            ))

An answer at r - Leaflet in Shiny - Stack Overflow kind of works except that it moves the map underneath the sidebarPanel, where as I would like it just in the mainPanel space.

ui <- fluidPage(
            sidebarLayout(
                sidebarPanel("sidebar panel"),
                mainPanel( bootstrapPage(
                             div(class = "outer", tags$style(type = "text/css", ".outer {position: fixed; top: 120px; left: 0; right: 0; bottom: 0; overflow: hidden; padding: 0}"),
                                 leafletOutput("map", width = "100%", height = "100%")
            )))))

Would plain width work in your use case? It adjusts the relative width of sidebar and main panel, the default ratio being 1:2; in this example I am applying 1:5 ratio (the widths are supposed to add up to 12, don't ask me why).

library(shiny)
library(leaflet)

ui <- bootstrapPage(
sidebarLayout(
    sidebarPanel("sidebar panel", width = 2),
    mainPanel(leafletOutput("map"), width = 10)
    ))

server <- function(input, output, session) {
    output$map <- renderLeaflet({
        leaflet() %>%
            addTiles() %>%  
            addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R") 
    })
}

shinyApp(ui, server)

Thanks for your answer @jlacko. Unfortunately, this suffers from the same issue as in the first example in my question; upon making it full screen the map is only in a small bit at the top. (it also show my question wasn't clear when i wrote "map is too small." -- sorry)

try using 100% of view height , like this :

leafletOutput("map",height="100vh")
2 Likes

Yes! Thanks @nirgrahamuk that is what I was hoping for. And you gave me further search terms.

(for others see CSS values and units - Learn web development | MDN to see what 'vh' is, and perhaps this javascript - How to get a leaflet map canvas to have a 100% height? - Stack Overflow)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.