首页 > 解决方案 > 允许用户以闪亮的方式上传 shapefile

问题描述

我正在制作一个闪亮的应用程序,允许用户使用sf包上传 shapefile。当我通过浏览窗口选择 .shp 文件时,我得到一个 .shp 文件error。我怎样才能允许用户上传一个 shapefile,然后让它读取st_read' or readOGR. 并且,我不知道为什么 st_read 会去,C:\Users\Ed\AppData...因为这不是 shapefile 的位置。

library(shiny)
library(shinydahsboard)
library(sf)

用户界面

ui = navbarPage("Project Eddy", theme = shinytheme("sandstone"),
                tabPanel("Location",
                sidebarLayout(sidebarPanel(fileInput("shp", "Please choose a Shapefile",
                                                      multiple = F,
                                                      ".shp")),
                mainPanel(plotlyOutput(outputId = "Area")))))

服务器

server = function(input, output, session) {
    
    myshp.df = reactive({
              
              # input$shp will be NULL initially. After the user selects
              # and uploads a file, head of that data file by default,
              # or all rows if selected, will be shown.
              
              req(input$shp)
              
              df = st_read(dsn = input$shp$datapath,
                           quite = T)
                                                             
              if(input$disp == "head") {
                return(head(df))
              }
              else {
                return(df)
              }
              
            })                                               
            
        output$Area = renderPlotly({
          req(myshp.df())
          a = myshp.df
          c = leaflet(a) %>%
            addPolygons(stroke = FALSE, fillOpacity = 0.5, smoothFactor = 0.5) %>%
            addProviderTiles('Esri.WorldImagery') 
        })
    })

错误

    Warning in CPL_read_ogr(dsn, layer, query, as.character(options), quiet,  :
      GDAL Error 4: Unable to open C:\Users\Ed\AppData\Local\Temp\RtmpioUU3m\b0cd5b1eb5c4fe4219e6c114\0.shx or C:\Users\Ed\AppData\Local\Temp\RtmpioUU3m\b0cd5b1eb5c4fe4219e6c114\0.SHX. Set SHAPE_RESTORE_SHX config option to YES to restore or create it.
    Warning: Error in : Cannot open "C:\Users\Ed\AppData\Local\Temp\RtmpioUU3m\b0cd5b1eb5c4fe4219e6c114\0.shp"; The source could be corrupt or not supported. See `st_drivers()` for a list of supported formats.
      128:

标签: rshinysf

解决方案


ESRI shapefile 是众所周知的麻烦制造者,因为它们存在于多个文件中 - 单个*.shp文件不足以让您的闪亮应用程序使用。

考虑用户 fiorepalombina 在 RStudio 社区论坛上提出的解决方案:https ://community.rstudio.com/t/shinyfiles-and-shapefiles/89099


推荐阅读