Help: Order of Plotting Shape files

I tried everything. This will be added into our app.
For now we need sf, maps and colourpicker. SO we cannot use ggplot2 leaflet etc.

The code actually works. However we want that the SHP layers from the ZIP File uploaded containing one or more shp projects can be plotted in different orders.
Meaning: The first layer selected will be the lowest the last layer selected will be on top. At the moment the order doesn't change. The last one added will always be on top.
(Similar to QGIS): I'haven't found any solution what so ever.

Please help. :frowning:

library(shiny)
library(maps)
library(sf)
library(colourpicker)

ui <- fluidPage(
  titlePanel("GIS Shapefile Overlay"),
  sidebarLayout(
    sidebarPanel(
      fileInput("shpFile", "Upload Shapefile (ZIP)"),  # File input to upload ZIP file
      uiOutput("checkboxes"),  # Dynamically generate checkboxes for shapefiles
      uiOutput("colorpickers"),  # Dynamically generate color pickers for each shapefile
      actionButton("plotButton", "Plot")
    ),
    mainPanel(
      plotOutput("mapPlot")
    )
  )
)

server <- function(input, output, session) {
  
  # Function to generate checkbox UI dynamically
  output$checkboxes <- renderUI({
    req(input$shpFile)
    zip_file <- input$shpFile$datapath
    unzip(zip_file, exdir = tempdir())
    shp_files <- list.files(tempdir(), pattern = ".shp$", full.names = TRUE)
    checkboxes <- lapply(shp_files, function(file) {
      file_name <- tools::file_path_sans_ext(basename(file))
      checkboxInput(paste0("shpCheckbox_", file_name), paste("Show", file_name), value = TRUE)
    })
    do.call(tagList, checkboxes)
  })
  
  # Function to generate color picker UI dynamically
  output$colorpickers <- renderUI({
    req(input$shpFile)
    zip_file <- input$shpFile$datapath
    unzip(zip_file, exdir = tempdir())
    shp_files <- list.files(tempdir(), pattern = ".shp$", full.names = TRUE)
    colorpickers <- lapply(shp_files, function(file) {
      file_name <- tools::file_path_sans_ext(basename(file))
      colourInput(inputId = paste0("shp_colour_", file_name), 
                  label   = paste("Color for", file_name),
                  value = "transparent", # default color
                  showColour = "background",
                  allowTransparent = TRUE,
                  palette = "square")
    })
    do.call(tagList, colorpickers)
  })
  
  output$mapPlot <- renderPlot({
    req(input$plotButton)
    
    if (is.null(input$shpFile))
      return(NULL)
    
    zip_file <- input$shpFile$datapath
    unzip(zip_file, exdir = tempdir())
    shp_files <- list.files(tempdir(), pattern = ".shp$", full.names = TRUE)
    
    if (length(shp_files) == 0) {
      cat("No shapefile found in the uploaded ZIP file.\n")
      return(NULL)
    }
    
    # Plot the world map with Europe bounds
    map("world", xlim = c(-10, 40), ylim = c(35, 70), interior = FALSE)
    
    # Iterate over each shapefile and plot if corresponding checkbox is checked
    for (file in shp_files) {
      file_name <- tools::file_path_sans_ext(basename(file))
      if (input[[paste0("shpCheckbox_", file_name)]]) {
        shape <- st_read(file)
        shape <- st_transform(shape, crs = st_crs("+proj=longlat +datum=WGS84"))
        plot(st_geometry(shape), add = TRUE, col = input[[paste0("shp_colour_", file_name)]])
      }
    }
  })
}

shinyApp(ui = ui, server = server)