R-Shiny中的自动多文件下载 [英] Automatic multi-file download in R-Shiny
问题描述
我正在尝试弄清楚如何获取data.frame
来本身作为子集,然后为每个子集编写一个.csv文件.我正在编写一个shiny
应用程序,它将为不同的仪器生成模板文件,并且我需要能够为每个批次/板/任何文件获取一个文件.显然,我们可以手动进行排序,但是这样做会破坏目的.
I'm trying to figure out how to get a data.frame
to subset itself and then write a .csv file for each subset. I'm writing a shiny
app which will generate template files for different instruments, and I need to be able to get a file for each batch/plate/whatever. Obviously, we could do a manual sort, but that kind of defeats the purpose.
例如,假设我有一个data.frame
,其中有4列,分别命名为1)PlateID,2)SampleName,3)Well和4)Comments,我想按PlateID进行子集化,以便每个单独的板都拥有自己的板文件.
In example, say that I have a data.frame
with 4 columns named 1) PlateID, 2) SampleName, 3) Well and 4) Comments and I want to subset by the PlateID such that each individual plate will have it's own file.
output$multiDownload <- renderText({
#templateData() just loads the data, nothing special;
#If you wanna see it let me know, but I think it's bulky
tempData <- templateData()
if(is.null(tempData)){return(NULL)}
#If there are more than one plate, subset by plate ID and write each file
if(max(tempData$PlateID) > 1){
for(i in 1:max(tempData$PlateID)){
tempSubsetForWrite <- subset(tempData, tempData$PlateID == i, select = c("names", "well", "comments"))
write.csv(tempSubsetForWrite, file = paste0("file ", i, " of ", max(tempData$PlateID), row.names = FALSE)
}
} else {
write.csv(tempData, file = "file", row.names = FALSE)
}
})
因此,我想添加一些功能,但不确定如何使用它们.首先,我想更好地控制数据的写入位置.我希望它与输入文件进入同一文件,但是我不确定如何强制执行此操作?我尝试做类似的事情:
So I want to add a few features and I'm not sure how to approach them. First, I would like to have better control of where the data is written. I would like it to go into the same file as my input files, but I'm not sure how to force this? I tried doing something like:
inFile <- input$templateGenerationFile
write.csv(tempData, paste0(inFile$datapath, "/file ", i, " of ", max(tempData$PlateID))
但在inFile$datapath
中似乎是生成的临时文件夹/文件,而不是直接链接到原始文件!
but in inFile$datapath
appears to be a temp folder/file which is generated and not a direct link to the original file!
此外,我想编写一种类似于downloadHandler
的功能,即具有一个按钮,单击该按钮可以在单击时下载文件,但是我认为在这种情况下我不能使用它,因为我正在写入多个文件.如果我错了,请让我知道,因为这样可以使生活更轻松.我想我将使用actionButton
和一个计数器变量,以使计数器成为按钮的值+ 1,直到按钮被激活为止,在这种情况下,它们在函数结束之前是相等的.显然,我将有条件处理其余部分,但这是微不足道的,所以让我们关注文件子集并下载吧!
Also, I'm wanting to write something that will act like a downloadHandler
in the sense of having a button which will download the files upon clicking, but I don't think I can use that in this scenario because I am writing multiple files. If I'm wrong then PLEASE let me know, as that would make life easier. I am thinking that I will use an actionButton
and a counter variable so that the counter is the value of the button + 1 until the button is activated, in which case they are equal until the end of the function. Obviously I would have a conditional which handles the rest, but that is trivial so lets focus on the file subset and download!
谢谢!
推荐答案
zip()
对我不起作用,但是使用tar
函数很容易..
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.
}
)
希望这会有所帮助.
我尝试使用zip
而不是tar
,但是出现错误:
hope this will help.
I try with zip
instead of tar
but I've got an error :
zip(file, paste("./dirwheremyfilesare/",dir("./dirwheremyfilesare"),sep=""))
谢谢.
安东尼奥·M.
这篇关于R-Shiny中的自动多文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!