我的闪亮应用程序中有一个上传文件按钮。我想上传shapefile并将其显示在我的传单地图中。我找到了此解决方案,但对我不起作用。 这是我的代码。
server.R:
library(shiny)
library(shinydashboard)
library(leaflet)
library(rgdal)
library(utf8)
library(reticulate)
shinyServer(function(input, output){
data <- reactive({
req(input$filedata)
})
map <- reactive({
req(input$filemap)
# shpdf is a data.frame with the name, size, type and datapath of the uploaded files
shpdf <- input$filemap
# Name of the temporary directory where files are uploaded
tempdirname <- dirname(shpdf$datapath[1])
# Rename files
for(i in 1:nrow(shpdf)){
file.rename(shpdf$datapath[i], paste0(tempdirname, "/", shpdf$name[i]))
}
# Read shp
map <- readOGR(paste(tempdirname, shpdf$name[grep(pattern = "*.shp$", shpdf$name)], sep="/"))
map
})
#Création de la carte
output$map <- renderLeaflet({
if(is.null(data()) | is.null(map())){
return(NULL)
}
map <- map()
data <- data()
# Add data to map
lesPolygones <- data()
lespolygones <- match(map@data)
map@data <- lesPolygones
leaflet() %>%
addProviderTiles("Esri.WorldImagery", group = "Esri World Imagery", options = providerTileOptions(minZoom = 2, maxZoom = 17)) %>%
addTiles(group = "OSM", options = providerTileOptions(minZoom = 2, maxZoom = 17)) %>%
addPolygons(
fillColor = "green",
color = "black",
dashArray = "3",
fillOpacity = 0.7
)
setView(lng = 96.221,lat = 20.792, zoom = 6 ) %>%
addLayersControl(baseGroups = c("Esri World Imagery", "OSM"))
})
})
UI.R:
shinyUI(
dashboardPage(
dashboardHeader(title ="Sen2extract"),
sidebar <- dashboardSidebar(
sidebarMenu(
fileInput(inputId = "inputData", label = "Importer un shape :", multiple = TRUE, accept = c('.shp', '.dbf', '.snb', '.sbx', '.shx', '.prj')),
)
),
dashboardBody(
tabItems(
tabItem(tabName ="carte",leafletOutput(outputId = "map", width="100%",height = "1100")),
)
)
)
)
目前,我的传单地图没有显示。当我运行该应用程序时,我没有错误消息。我是R的初学者,所以我寻求您的帮助。谢谢!