R Shiny-将csv下载到工作目录 [英] R Shiny - downloading a csv to the working directory
问题描述
我有一个Shiny应用程序,我想在其中完成以下任务:
1)用户按下按钮
2)将数据框导出到.csv,保存在工作目录(带有server.R和ui.R)中,或者最好向下一级保存.
我希望这种情况能够自动发生,因为最终我要将其与checkboxGroupboxInput连接起来以遍历数据并生成一组过滤的.csv.
这是我目前可以获得的最接近的数据,其中fileToDownload代表我的数据框:
ui.R:
downloadButton("rptDownload","Download Report"),
server.R:
output $ rptDownload<-downloadHandler(文件名= function(){粘贴(getwd(),'/rpt/test.csv',sep ='')},内容=函数(con){write.csv(fileToDownload,file = file.path(con))})
当我从Chrome浏览器中运行该文件时,它会下载到.csv文件中,但它以我想成为文件路径的名称来命名,即名称的一部分:C--Work-Project_A-Shiny_DB-rpt-test.csv.它将getwd()和/rpt/视为名称的一部分,而不是文件路径.它会下载到我的下载文件夹中,而不是当前的工作目录中.
关于如何改进我的代码的任何建议?
请注意,我尝试合并其他线程的一些建议.除其他外:
Shiny(R):将selectInput中的选择下载到本地机器
https://groups.google.com/forum/#!topic/shiny-discuss/MaN4-ia6wfk
但是这些似乎使我远离完成我想做的事情.感谢您的协助.
我认为您想要的是用户单击按钮并让 shiny
将数据保存到您指定的某个文件夹中.这是正确的吗?
如果这是您想要的,则创建一个按钮,当用户单击该按钮时,该按钮将只是 write.csv(< DF-to-write> ;,<要使用的文件名>)代码>或类似的内容.
无需使用 downloadHandler
. downloadHandler
函数特定于导出数据:用户单击此按钮,您的应用程序将为其导出数据.以这种方式使用时,数据显示在 Download
文件夹中的原因是因为您已将浏览器设置为将所有下载的文件存储到 Download
目录中.
如果需要,可以有一个按钮用于 SaveSelectedData
,而另一个按钮用于 DownloadSavedData
.
-
SaveSelectedData
将感兴趣的dfwrite.csv
到您指定的文件夹. -
DownloadSavedData
会将数据导出到您的用户(尽管您无法指定将数据下载到用户系统上的位置,这取决于他们的浏览器设置).
还请记住,在服务器上运行时,您需要确保Shiny有权写入您想要将文件保存到的文件夹.
I’ve got a Shiny app in which I’d like to accomplish the following:
1) User presses a button
2) A data frame gets exported to a .csv, saved in either the working directory (with server.R and ui.R), or ideally one level down.
I want this to happen automatically, because eventually I’m going to connect it with a checkboxGroupboxInput to loop through the data and produce a set of filtered .csv.
Here is the closest I can currently get, with fileToDownload representing my data frame:
ui.R:
downloadButton("rptDownload", "Download Report"),
server.R:
output$rptDownload <- downloadHandler(
filename = function() {
paste(getwd(), '/rpt/test.csv',sep = '')
},
content = function(con) {
write.csv(fileToDownload, file = file.path(con))
}
)
When I run this from a Chrome browser, it downloads to a .csv, but it gets named with what I want to be the file path as part of the name: C--Work-Project_A-Shiny_DB-rpt-test.csv. It regards the getwd() and /rpt/ as part of the name instead of the file path. It gets downloaded to my Downloads folder, not the current working directory.
Any suggestions on how to improve my code?
Note that I have tried incorporating some suggestions from other threads. Among others:
Shiny (R): Download selection in selectInput to local machine
Automatic multi-file download in R-Shiny
https://groups.google.com/forum/#!topic/shiny-discuss/MaN4-ia6wfk
But these seem to be moving me further away from accomplishing what I'm trying to do. Thanks for any assistance.
I think what you want is for the user to click a button and have shiny
save the data to some folder which you specify. Is this correct?
If that's what you want, then create a button which, when clicked by the user, will just write.csv(<DF-to-write>, <filename-to-use>)
or something along those lines.
No need to use downloadHandler
. The downloadHandler
function is specific to exporting the data: The user clicks on this button and your app exports the data for them. The reason the data shows up in your Download
folder when you use it this way is because you've set up your browser to store all downloaded files to your Download
directory.
If you want, you can have one button for SaveSelectedData
and another button for DownloadSavedData
.
SaveSelectedData
willwrite.csv
the df's of interest to your specified folder.DownloadSavedData
will export the data out to your user (you can't specify where it will get downloaded to on your user's system though -- that's dependent on their browser settings).
Also keep in mind that when running on the server, you'll need to make sure that Shiny has permissions to write to the folder you want it to save files to.
这篇关于R Shiny-将csv下载到工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!