0

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 shinyapp, 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.

Thanks for help !

EDIT with smartse solution, I get an error :

enter image description here

1 Answer 1

0

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

12
  • Hello, it works to display "All" but not the individual members, I get the following error : Warning: Error in if: argument is of length zero [No stack trace available]
    – Chika
    Commented Aug 4, 2023 at 7:49
  • That's strange - I checked that before and I've just checked it again and it works - see the screenshots I've added. Are you just running the example?
    – smartse
    Commented Aug 4, 2023 at 8:21
  • Yes the exact same example and I also copied your code. I've updated my initial post with the code and the error
    – Chika
    Commented Aug 4, 2023 at 13:25
  • Hmm that is strange. The error means that input$nb is empty which it shouldn't be... but you could try adding req(input$nb) as the first line in the observe and see whether that makes a difference - that checks that input$nb isn't empty so should stop if returning an error.
    – smartse
    Commented Aug 4, 2023 at 14:48
  • It is not empty because it returns "10" from the print I've added just before the output$map. No difference, exact same error if I add "req(input$nb)", very strange...
    – Chika
    Commented Aug 4, 2023 at 15:17

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