如何解决无法在闪亮的应用程序中打开shp文件的数据源
我下面有一个闪亮的应用程序,其中,我试图上传.shp文件,并在转换为数据框后将其显示在表格中。我遵循了此链接here,但出现错误:Error in ogrListLayers: Cannot open data source
。在闪亮的应用程序外部,此方法有效:map <- readOGR("MT_Flathead_County_30029_ec88b737.shp")
我猜这与here之类的路径有关。我认为我可能需要像这种方法一样读取完整的数据路径:
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
})
APP
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。