R 无法在应用程序中打开shp文件的数据源
我有一个闪亮的应用程序,我试图上传.shp文件,并在转换成数据帧后显示在表格中。我跟随了这个链接,但我得到了错误:R 无法在应用程序中打开shp文件的数据源,r,shiny,rgdal,R,Shiny,Rgdal,我有一个闪亮的应用程序,我试图上传.shp文件,并在转换成数据帧后显示在表格中。我跟随了这个链接,但我得到了错误:ogrListLayers中的错误:无法打开数据源。在闪亮的应用程序之外,这是有效的:map inShp <- reactive({ # shpdf is a data.frame with the name, size, type and datapath # of the uploaded files shpdf <- input$filemap #
ogrListLayers中的错误:无法打开数据源
。在闪亮的应用程序之外,这是有效的:map
inShp <- reactive({
# shpdf is a data.frame with the name, size, type and datapath
# of the uploaded files
shpdf <- input$filemap
# The files are uploaded with names
# 0.dbf, 1.prj, 2.shp, 3.xml, 4.shx
# (path/names are in column datapath)
# We need to rename the files with the actual names:
# fe_2007_39_county.dbf, etc.
# (these are in column name)
# 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])
)
}
# Now we read the shapefile with readOGR() of rgdal package
# passing the name of the file with .shp extension.
# We use the function grep() to search the pattern "*.shp$"
# within each element of the character vector shpdf$name.
# grep(pattern="*.shp$", shpdf$name)
# ($ at the end denote files that finish with .shp,
# not only that contain .shp)
map <- readOGR(paste(tempdirname,
shpdf$name[grep(pattern = "*.shp$", shpdf$name)],
sep = "/"
))
map
})
library(shiny)
library(rgdal)
library(DT)
options(shiny.maxRequestSize = 40*1024^2)
# ui object
ui <- fluidPage(
titlePanel(p("Spatial app", style = "color:#3474A7")),
sidebarLayout(
sidebarPanel(
fileInput(
inputId = "filemap",
label = "Upload map. Choose shapefile",
multiple = TRUE,
accept = c(".shp", ".dbf", ".sbn", ".sbx", ".shx", ".prj")
)
),
mainPanel(
DTOutput(outputId = "table")
)
)
)
# server()
server <- function(input, output) {
inShp = reactive({
map<-readOGR(input$filemap$datapath)
data.frame(map)
})
output$table <- renderDT(inShp())
}
# shinyApp()
shinyApp(ui = ui, server = server)