Automatic multi-file download in R-Shiny

时光总嘲笑我的痴心妄想 提交于 2019-12-01 08:12:15

The zip() doesn't work for me but it is easy with the tar function..

      output$downloadData <- downloadHandler(
         filename = function() { paste("filesintar", '.tar', sep='') },
         content = function(file) {
           tar(file,"./dirwheremyfilesare") #  no inlcuye rutas hasta raíz.
         }
       )

hope this will help. I try with zip instead of tar but I've got an error :

        zip(file, paste("./dirwheremyfilesare/",dir("./dirwheremyfilesare"),sep=""))

Thank u.

Antonio M.

I modified @amaurandi answer to complete OP question, adding some extra decorations that I found worked for me. Warning, I also used a plyr function, dlply, to simplify a bit.

I reproduced the problem with zip throwing an error (cannot explain it), but tar can be made to work. In principle these are platform independent functions in R, but that also implies everything is setup correctly with your installation.

Example based on OP starting point and previous answer snippet from @amaurandi

output$multiDownload <- downloadHandler(
  filename = function(){
    # Time-stamp tar filename
    paste0("platetables-", gsub("\\D", "_", Sys.time()), ".tar")
  },
  content = function(file){
    # Loads the data. Could be reactive.
    tempData <- templateData()
    if(is.null(tempData)){
      # Protect against empty data.
      return(NULL)
    }
    plateNames = unique(tempData$PlateID)
    tempdir = tempdir()
    dlist = plyr::dlply(tempData, "PlateID", function(x) x[, c("names", "well", "comments")])
    for(i in plateNames){
      write.csv(x = dlist[[i]], file = paste0(tempdir, "/", i, ".csv"), row.names = FALSE)
    }
    tar(tarfile = file, files = tempdir)
  }
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!