Below is a reprex of a Shiny app. The reprex has been adapted from the vignette of the Mapdeck library (https://symbolixau.github.io/mapdeck/articles/tips_tricks.html#shiny-1).
The app creates a Mapdeck map and has to action buttons:
- “roads” which adds the road layer to the map.
- “zones” which add a geojson file with polygons to the map.
Mapdeck displays the layers in the order in which the layers are clicked:
- If “roads” is clicked first, followed by “zones”, then the “roads” layer is displayed at the bottom with the “zones” on top.
- If “zones” is clicked first, followed by “roads”, then the “zones” layer is displayed at the bottom with the “zones” on top.
Is it possible to modify the app so that “zones” are always displayed at the bottom, with “roads” always on top, irrespective of the order in which the layers are added?
Herewith the reprex:
library(shiny)
library(shinydashboard)
library(mapdeck)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
actionButton(inputId = "roads", label = "roads"),
actionButton(inputId = "zones", label = "zones")
),
dashboardBody(
mapdeckOutput(outputId = "map")
)
)
server <- function(input, output) {
## initialise a map
output$map <- renderMapdeck({
mapdeck()
})
## use an observer to add and remove layers
observeEvent(input$roads,{
if ( input$roads %% 2 == 1 ) {
mapdeck_update(map_id = "map") %>%
add_path(
data = roads,
layer_id = "myRoads",
stroke_colour = "RIGHT_LOC",
update_view = TRUE
)
} else {
mapdeck_update(map_id = "map") %>%
clear_path(layer_id = "myRoads")
}
})
## use an observer to add zones
observeEvent(input$zones, {
if ( input$zones %% 2 == 1 ) {
mapdeck_update(map_id = "map") %>%
add_geojson(
data = geojson,
layer_id = "myZones",
update_view = TRUE)
} else {
mapdeck_update(map_id = "map") %>%
clear_geojson(layer_id = "myZones")
}
})
}
shinyApp(ui, server)