Configure a FlyToBounds for an uploaded shapefile

I would like to change the view of my map when upload a zipped shapefile. For that, I think that the function "flyToBounds" can be useful, but I do not know how to adapt it to my code. I have a function to add a leaflet map, then a function to read the zipfile and another to add the shapefile on this map.

   # server.R

#--------- ADD PACKAGES --------#
library(shiny)
library(shinydashboard)
library(leaflet)
library(rgdal)
library(rgeos)
library(sp)
library(leaflet.extras)
library(raster)


#--------- APPLICATION INSTRUCTIONS --------#
shinyServer(function(input, output, session){



  #--------- ADD LEAFLET MAP --------#
  output$map <- renderLeaflet({
    leaflet() %>%
      enableTileCaching() %>%
      addTiles(group = "OSM", urlTemplate = "https://{s}.tile.openstreetmap.de/tiles/osmde/{z}/{x}/{y}.png", 
               attribution = '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors', 
               options = providerTileOptions(minZoom = 2, maxZoom = 17), 
               tileOptions(useCache = TRUE, crossOrigin = TRUE)) %>%
      setView(lng = 97.963,lat = 20.380, zoom = 6 )
  })

  #-------- READ ZIP --------#
  uploadShpfile <- reactive({
    if (!is.null(input$zip)) {
      zipFile <- input$zip
      zipPath <- substr(zipFile$datapath, 1, nchar(zipFile$datapath) - 5)
      unzip(zipFile$datapath, exdir = zipPath)
      pwd <- getwd()
      updir <- dirname(zipFile$datapath[1])
      setwd(updir)
      for (i in 1:nrow(zipFile)) {
        file.rename(zipFile$datapath[i], zipFile$name[i])
      }
      shpName <- zipFile$name[grep(zipFile$name, pattern = "*.shp")]
      shpPath <- paste(updir, shpName, sep = "/")
      setwd(updir)
      Layers <- ogrListLayers(shpPath)
      shpName <- readOGR(shpPath)
      shpName <- spTransform(shpName,CRS("+proj=longlat +datum=WGS84"))
      shapefile(shpName, paste(shpPath, Layers, "_WGS84.shp", sep = ""))
      shpName
    }
  })


  #-------- ADD SHAPEFILE --------#
  observeEvent(input$zip, {

    data = uploadShpfile()
    map = leafletProxy("map")
    if (!is.null(uploadShpfile())){
      if(inherits(data, "SpatialPolygons")){
        shinyalert("Successful upload !", type = "info", timer = 2000)
        cent <- gCentroid(spgeom = uploadShpfile(), byid = FALSE)
        leafletProxy("map")%>%
          addPolygons(data = uploadShpfile(),
              stroke = TRUE,
              # color = "#00FFEC",
              # fillColor = "white",
              fillOpacity = 0.5) 
        }

      if(inherits(data, "SpatialPoints")){
        shinyalert("Successful upload !", type = "info")
        cent <- gCentroid(spgeom = uploadShpfile(), byid = FALSE)
        leafletProxy("map") %>%
          addCircleMarkers(data = uploadShpfile(),
              stroke = TRUE,
              # color = "white",
              # fillColor = "#00FFEC",
              radius = 6,
              fillOpacity = 0.9) 
        } 
    }
  })
})


    # ui.R

#--------- ADD PACKAGES --------#
library(shiny)
library(shinydashboard)
library(leaflet)
library(rgdal)
library(rgeos)
library(sp)
library(leaflet.extras)
library(raster)



#--------- USER INTERFACE --------#
shinyUI(
  dashboardPage(
    dashboardHeader(title =""),

    sidebar <- dashboardSidebar(
      sidebarMenu(
        menuItem("Map", tabName= "carte", icon = icon("globe"))
      )
    ),
    dashboardBody(

      tabItems(
        #--------- ELEMENTS TAB "carte" --------#
        tabItem(tabName ="carte",
                fluidRow(

                  box(
                    width = 3,
                    title = "Settings",
                    status = "primary",
                    solidHeader = TRUE,
                    collapsible = TRUE,
                    fileInput(inputId = "zip", label = "Upload your zipped shapefile :", multiple = FALSE, accept = c('.zip'))
                    ),

                  box(
                    width = 9,
                    title = "Map",
                    status = "primary",
                    solidHeader = TRUE,
                    collapsible = FALSE,
                    height = "100%",
                    leafletOutput(outputId = "map", width="100%", height = 940)
                  )
                )
        )
      )
    )
  )
)

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