从Shiny

时间:2017-01-17 21:59:18

标签: r shiny

我有一个Shiny应用程序,它读取gpx跟踪文件并缓冲它。然后,我希望用户能够将该shapefile下载到他们选择的目的地。我一直在尝试使用downloadHandler函数,但到目前为止我没有快乐。

我创建的shapefile的名称称为trk_buff。 在R中我可以使用:

my_dsn<-"C:\\Documents\\TrackFiles"
writeOGR(obj=trk_buff, dsn=my_dsn, layer="BufferedTracks", driver="ESRI Shapefile")

我试图使用downloadHandler:

  output$downloadData<-downloadHandler(
    filename=function(file){trk_buff},
    content=function(file){
      writeOGR(obj=trk_buff, dsn=file, layer="BufferedTracks", driver="ESRI Shapefile")
    })

但是我显然做错了什么都没有发生......:

编辑添加 如果我使用动作Button和textFile框的组合,我可以得到我想要的行为。 但这有点笨拙并且涉及用户明确地编写文件路径而不是搜索它,这可能会导致错误: 例如 在ui.R我有:

textInput("filepath","Filepath to download data"),
actionButton("act1","Download shapefile")

在server.R我有:

  action_btn_code <- eventReactive(input$act1, {
    file_path<-input$filepath    
    writeOGR(obj=trk_buff, dsn=paste(file_path,"/Tracks",sep=""), layer="BufferedTracks", 
      driver="ESRI Shapefile", overwrite_layer=TRUE)
  })

2 个答案:

答案 0 :(得分:3)

以下适用于我。我们的想法是你必须压缩shapefile,因为downloadHandler只能处理下载一个文件。

output$download_shp <- downloadHandler(
    filename = "shapefile.zip",
    content = function(file) {
        data = trk_buff() # I assume this is a reactive object
        # create a temp folder for shp files
        temp_shp <- tempdir()
        # write shp files
        writeOGR(data, temp_shp, "trk_buff", "ESRI Shapefile", 
                 overwrite_layer = TRUE)
        # zip all the shp files
        zip_file <- file.path(temp_shp, "trk_buff_shp.zip")
        shp_files <- list.files(temp_shp,
                                "trk_buff",
                                full.names = TRUE)
        # the following zip method works for me in linux but substitute with whatever method working in your OS 
        zip_command <- paste("zip -j", 
                             zip_file, 
                             paste(shp_files, collapse = " "))
        system(zip_command)
        # copy the zip file to the file argument
        file.copy(zip_file, file)
        # remove all the files created
        file.remove(zip_file, shp_files)
    }
)

答案 1 :(得分:1)

library(sf)
library(zip)

output$download_shp <- downloadHandler(
    filename <- function() {
        "Data_shpExport.zip"
        
    },
    content = function(file) {
        withProgress(message = "Exporting Data", {
            
            incProgress(0.5)
            tmp.path <- dirname(file)
            
            name.base <- file.path(tmp.path, "BufferedTracks")
            name.glob <- paste0(name.base, ".*")
             name.shp  <- paste0(name.base, ".shp")
            name.zip  <- paste0(name.base, ".zip")
            
            if (length(Sys.glob(name.glob)) > 0) file.remove(Sys.glob(name.glob))
            sf::st_write(trk_buff, dsn = name.shp, ## layer = "shpExport",
                      driver = "ESRI Shapefile", quiet = TRUE)
            
            zip::zipr(zipfile = name.zip, files = Sys.glob(name.glob))
            req(file.copy(name.zip, file))
            
            if (length(Sys.glob(name.glob)) > 0) file.remove(Sys.glob(name.glob))
            
            incProgress(0.5)
        })
    }  
)