簡體   English   中英

R-從Shiny應用程序下載shapefile

[英]R - download shapefile from a Shiny app

我為如何使用shapefile的下載按鈕設置閃亮的應用程序而感到困惑。

我在這里關注了參考

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

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

創建zip文件:錯誤的運行命令“”的狀態為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")

您的操作系統不知道如何使用zip命令(狀態127)。 在Unix系統上,此解決方案應該有效。 在Windows上,您必須下載一個zip程序或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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM