rshinymapsr-leaflet

Display multiple leaflet maps on the same plot


I have a shiny app where the user can select to display a specific leaflet map (on 51 available), or all leaflet maps (the 51 on 11 columns and 5 rows).

To display multiple leaflet maps on the same plot, I am using latticeView, which works well outside of the shiny app, but which is not displayed with renderleaflet.

Which render should I use? Is it possible to use the same render for both the multiple map and the single maps as it is the same plot ID?

REPREX :

require(leaflet)
require(leafsync)
require(shiny)

tilesURL <- "http://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/tile/{z}/{y}/{x}"

basemap <- list()
for(n in 1:51) {
  basemap[[n]] <- basemap1 <- leaflet() %>%
    addTiles(tilesURL) 
}



ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("nb", "Select number", choices = c("All", 1:51))
    ),
    
    mainPanel(
      leafletOutput("map")
    )
  )
)

server <- function(input, output, session) {
  
  observe({
    if(input$nb=="All") {
      output$map <- renderLeaflet({
        latticeView(basemap, ncol=11)
      })
    } else {
      output$map <- renderLeaflet({
        basemap[[as.numeric(input$nb)]]
      })
    }
    
  })
}

shinyApp(ui, server)

This code displays the maps if we select the number between 1 and 51, but not if we select "All" as input.


Solution

  • Swapping to renderUI and uiOutput instead seems to work:

    mainPanel(
     uiOutput("map")
    )
    
      observe({
        if(input$nb=="All") {
          output$map <- renderUI({
            latticeView(basemap, ncol=11)
          })
        } else {
          output$map <- renderUI({
            basemap[[as.numeric(input$nb)]]
          })
        }
    

    enter image description here enter image description here