使用 writeogr() 下载 Shapefile 时出现 R Shiny downloadHandler() 错误

标签 r shiny

我是 R shiny 的新手,正在创建一个 Shiny 应用程序来将 CSV 文件转换为 shapefile。运行我的代码时出现此错误:

enter image description here

我的代码:

runApp(list(
  ui = bootstrapPage(
      fileInput('inputdata', 'Input file',accept=c('.csv')),
      plotOutput("xyPlot"),
      downloadButton('downloadShp', 'DownloadSHP')
       ),
  server = function(input, output) {

  output$xyPlot <-  renderPlot({

  myXY<- input$inputdata
  if (is.null(myXY)) 
    return(NULL)       

  xyPoints<-read.table(myXY$datapath, sep=",", header=T)

  SHP <- SpatialPointsDataFrame(coords= cbind(xyPoints[,1:2]), data =  xyPoints)
  proj4string = CRS('+proj=longlat +datum=NAD83')

  plot(SHP)

  output$downloadShp <- downloadHandler(
    filename = 'fbCrawlExport.zip',
    content = function(file) {
      if (length(Sys.glob("fbCrawl.*"))>0){
        file.remove(Sys.glob("fbCrawl.*"))
      }
      writeOGR(SHP, dsn="fbCrawl.shp", layer="fbCrawl", driver="ESRI Shapefile")
      write.csv(as.data.frame(cbind(SHP@data, as.data.frame(SHP@coords))), "fbCrawl.csv")
      zip(zipfile='fbCrawlExport.zip', files=Sys.glob("fbCrawl.*"))
      file.copy("fbCrawlExport.zip", file)
      if (length(Sys.glob("fbCrawl.*"))>0){
        file.remove(Sys.glob("fbCrawl.*"))
      }
    }
  )

  }) 
 }
 ))

最佳答案

我使用 Rtools 解决了这个问题。 这是我更新的代码

require(shiny)
require(sp)
require(rgdal)
Sys.setenv("R_ZIPCMD" = "C:/Rtools/bin/zip.exe")

runApp(
  list(
ui = bootstrapPage(
  fileInput('inputdata', 'Input file',accept=c('.csv')),
  downloadButton('downloadShp', 'DownloadSHP')
),
server = function(input, output) {

  createShp <- reactive({
    myXY <- input$inputdata
    if (is.null(myXY)){
      return(NULL)      
    } else {
      xyPoints <- read.table(myXY$datapath, sep=",", header=T)

      SHP <- SpatialPointsDataFrame(coords= cbind(xyPoints[,1:2]), data =  xyPoints)
      proj4string(SHP) <- CRS("+init=epsg:4326")
      return(SHP)
    }
  })

  output$downloadShp <- downloadHandler(

    filename = function() { paste0("shpExport.zip") }, #paste('shpExport.zip',
    content = function(file) {
      if (length(Sys.glob("shpExport.*"))>0){
        file.remove(Sys.glob("shpExport.*"))
      }
      writeOGR(createShp(), dsn="shpExport.shp", layer="shpExport", driver="ESRI Shapefile")
      zip(zipfile='shpExport.zip', files=Sys.glob("shpExport.*"),zip = Sys.getenv("R_ZIPCMD", "zip"))
      file.copy("shpExport.zip", file)
      if (length(Sys.glob("shpExport.*"))>0){
        file.remove(Sys.glob("shpExport.*"))
      }
    }
  )

}) 
)

关于使用 writeogr() 下载 Shapefile 时出现 R Shiny downloadHandler() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44032431/

相关文章:

r - 将 dplyr 过滤条件从 Shiny 的输入传递到函数中

r - Shiny :单击图像时更改选项卡

r - 如何使用从 group_by (dplyr) 中退休的值创建列

r - 如何在列表列上应用一个函数并在 dplyr 和 purrr 中返回另一个函数?

r - Knitr:不要注释掉未评估的代码

r - 数据框中每两行之间的余弦相似度

r - 虚拟机上带有 Rstudio 的 mxnet 包

r - 当应用程序繁忙时如何防止用户在 Shiny 的应用程序上做任何事情

r - 防止 Shiny 更新 plotly 的元素

r - Plotly "drawopenpath"成品线颜色与绘图过程中的颜色