我有一个shiny
应用程序,我想通过一个按钮下载一个文件。因此,我可以通过调用downloadHandler
来完成此操作,如下所示:
output$downloadData <- downloadHandler(
filename = "plot1.png",
content = function(file) {
plotPNG(func = function(){
plot(some.Data)
},
filename = file,
width = 3000,
height = 2000,
res = 300
)
}
)
在这里,文件被下载到默认的下载目录。现在如果我想下载到另一个目录怎么办?换句话说:有没有一种方法可以确定默认的下载目录并对其进行操作?
发布于 2018-09-19 22:36:56
downloadHandler()应该能够处理这个问题。filename参数是“包括扩展名的文件名字符串,用户的web浏览器在下载文件时应默认使用该字符串。”因此,我们可以通过在文件名中定义路径来操作默认目录。
server<-function(input,output){
output$downloadData <- downloadHandler(
# Sets filename the browser should default too
filename = function() {
paste(PATH_TO_DIR,"plot1",".png/xls/etc.",sep="")
}, # Closes Filename Function
# Creates Download file
content = function(file) {
plotPNG(func = function(){
plot(some.Data)
},
file = filename(),
width = 3000,
height = 2000,
res = 300
)
}
ui<-
downloadButton("downloadData", 'Download File')
)
这可以在带有download按钮的co-op中使用,并且当按下按钮时,可以通过使用变量定义来动态生成文件和路径的路径名。我想这可能会打开一个文件浏览器,我不知道这是否可以避免。
另请注意,文件名、路径和内容类型在本地会话(RStudio)中不起作用,它们只有在部署到浏览器中后才起作用。
https://stackoverflow.com/questions/52383114
复制相似问题