The application I created allows me to import a polygon shapefile into my Leaflet map. However, I would like to set up a point shapefile import on the same button. I tried several attempts, especially with "addCircles". One or the other works, but not both at the same time. How could I go about it? Here is my code:
ui.R
shinyUI(
dashboardPage(
#bandeau principal
dashboardHeader(title ="Sen2extract"),
#menu a gauche
sidebar <- dashboardSidebar(
sidebarMenu(
#Les differents widgets du menu
menuItem("Carte", tabName= "carte", icon = icon("globe")),
fileInput(inputId = "shp", label = "Importer un shape :", multiple = TRUE, accept = c('.shp', '.dbf','.sbn', '.sbx', '.shx', '.prj')),
actionButton(inputId ="reset", label = "", icon = icon("trash-alt")),
selectInput(inputId ="dataset", label ="Choisir un indice :", choices = c("NDVI", "NDWI GAO", "NDWI MCF", "MNDWI")),
br(),
dateRangeInput(inputId ="dates", label = "Selectionner les dates :", start = "2019-01-01"),
submitButton("Rechercher", icon("cog", lib = "glyphicon")),
downloadButton("downloadData", label = "Exporter en CSV"),
br(),br(),
menuItem("A propos", tabName = "about", icon = icon("list-alt")),
menuItem("FAQ", tabName = "faq", icon = icon("question-sign",lib= "glyphicon"))
)
),
#contenu de l'application
dashboardBody(
tabItems(
tabItem(tabName ="carte",leafletOutput(outputId = "map", width="100%",height = "1100")),
tabItem(tabName ="about",h1("Bienvenue sur l'application sen2extract !"),
p("Cet outils permet l'extraction de series temporelles des indices spectraux sur differents sites d'etudes.")),
tabItem(tabName ="faq", h1("F.A.Q"), h3("Mon shape ne s'importe pas, comment faire ?"),
p("Referez-vous aux indications de l'onglet Description"),
h3("L'extraction de ma serie temporelle est anormalement longue, est-ce normal ?"),
p("Il est possible que l'extraction prenne du temps. Cela va dependre du nombre de polygones en entree, ainsi que de la plage temporelle configuree"))
)
)
)
)
server.R :
library(shiny)
library(shinydashboard)
library(leaflet)
library(rgdal)
library(utf8)
library(sf)
library(rgeos)
library(sp)
shinyServer(function(input, output, session){
# create reactive upload file function to store data
uploadShpfile <- reactive({
if (!is.null(input$shp)) {
shpDF <- input$shp
pwd <- getwd()
updir <- dirname(shpDF$datapath[1])
setwd(updir)
for (i in 1:nrow(shpDF)) {
file.rename(shpDF$datapath[i], shpDF$name[i])
}
shpName <- shpDF$name[grep(x = shpDF$name, pattern = "*.shp")]
shpPath <- paste(updir, shpName, sep = "/")
setwd(pwd)
shpFile <- readOGR(shpPath)
shpFile <- spTransform(shpFile,CRS("+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0"))
}
})
output$map <- renderLeaflet({
leaflet() %>%
addProviderTiles("Esri.WorldImagery", group = "Esri World Imagery", options = providerTileOptions(minZoom = 2, maxZoom = 17)) %>%
addTiles(group = "OSM", options = providerTileOptions(minZoom = 2, maxZoom = 17)) %>%
addMiniMap(toggleDisplay = T) %>%
setView(lng = 97.963,lat = 20.380, zoom = 6 ) %>%
addLayersControl(baseGroups = c("Esri World Imagery", "OSM"))
})
observeEvent(input$shp, {
if (!is.null(uploadShpfile())) {
cent <- gCentroid(spgeom = uploadShpfile(), byid = FALSE)
leafletProxy("map") %>%
addCircleMarkers(data = uploadShpfile())
addPolygons(data = uploadShpfile()) %>%
setView(lat = slot(cent, "coords")[[2]], lng = slot(cent, "coords")[[1]], zoom = 7)
}
})
})
addCircleMarkers
andaddPolygons
lines.Warning: Error in polygonData.default: Don't know how to get path data from object of class SpatialPointsDataFrame.
I tried to import your code below, but it doesn't work too. I'm a R rookie