R - 从闪亮的应用程序

时间:2017-12-01 10:12:13

标签: r shiny gis spatial shapefile

我很难过如何使用shapefile的下载按钮设置我的闪亮应用。

我已经按照这里的参考资料

https://groups.google.com/forum/#!topic/shiny-discuss/q3-2O6JDk74

https://gist.github.com/RCura/b6b1759ddb8ab4035f30

Create zip file: error running command " " had status 127

这就是我所拥有的:

ui <- fluidPage(  
  sidebarLayout(
    sidebarPanel(
      textInput("downloadShp","Filename:",value="fbCrawlExport.zip"),
      downloadButton('fbCrawlExport.zip', 'DownloadSHP')
    )))


server <- function(input, output) {
  output$fbCrawlExport.zip <- downloadHandler(
    filename = 'fbCrawlExport.zip',
    content = function(file) {
      if (length(Sys.glob("fbCrawl.*"))>0){
        file.remove(Sys.glob("fbCrawl.*"))
      }
      proj4string(Fg_filt2) <- CRS("+proj=longlat +datum=WGS84")
      writeOGR(Fg_filt2, dsn="fbCrawl.shp", layer="fbCrawl", driver="ESRI Shapefile")
      write.csv(as.data.frame(cbind(Fg_filt2@data, as.data.frame(Fg_filt2()@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.*"))
      }
    }
  )

我现在唯一可以向前推进的方法是下载一个csv。

ui <- fluidPage(  
  textInput("downloadData","Filename:",value="filename.csv"),
      downloadButton('downloadData','Save')
    )

server <- function(input, output) {
  output$downloadData <- downloadHandler(
    filename = input$downloadData,
    content = function(file) {
      write.csv(Fg_filt2,file)
    }
  )
}

然而,我可以轻松地使用

创建一个有光泽的shapefile
proj4string(myshp) <- CRS("+proj=longlat +datum=WGS84")
writeOGR(myshp, "C:/Data/ShinyApps", "myshp_wgs84", driver = "ESRI Shapefile")

1 个答案:

答案 0 :(得分:0)

您的操作系统不知道如何处理zip命令(状态127)。在Unix系统上,这个解决方案应该可行。在Windows上,您必须下载zip-programm或Rtools并将 \ bin 文件夹附加到系统变量。 (可以是C:\ RBuildTools \ 3.4 \ bin或C:\ RBuildTools \ bin或安装它的任何地方)。

您可以打开命令提示符进行检查,然后输入zip。当您收到错误消息时,出现问题,如果您看到其他选项,那就很好。

重新启动R 并尝试以下代码。

运行代码,在浏览器中打开ShinyApp ,看看它是否有效。

library(shiny); library(leaflet)

Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2))); Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5))); Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)
Srs1 = Polygons(list(Sr1), "s1"); Srs2 = Polygons(list(Sr2), "s2"); Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)
SpDF <- SpatialPolygonsDataFrame(SpP, data.frame(ID = 1:3, p = runif(3,5,10), name=c("a","b","c")), match.ID = F)


server <- function(input, output) {
  output$map <- renderLeaflet({
    leaflet() %>% 
      addTiles("http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png") %>% 
      addPolygons(data=SpDF)
  })


  output$ShapeExport <- downloadHandler(
    filename = 'ShapeExport.zip',
    content = function(file) {
      if (length(Sys.glob("shape_export.*"))>0){
        file.remove(Sys.glob("shape_export.*"))
      }
      writeOGR(SpDF, dsn="shape_export.shp", layer="shape_export", driver="ESRI Shapefile", overwrite_layer = T)
      write.csv(as.data.frame(SpDF@data), "shape_export.csv")
      zip(zipfile='ShapeExport.zip', files=Sys.glob("shape_export.*"))
      file.copy("ShapeExport.zip", file)
      if (length(Sys.glob("shape_export.*"))>0){
        file.remove(Sys.glob("shape_export.*"))
      }
    }
  )
}

ui <- fluidPage(
  downloadButton('ShapeExport', 'DownloadSHP'),
  leafletOutput("map")
)
shinyApp(ui, server)