我有一个downloadButton
与downloadHandler
,当我们要下载数据,我必须运行一个程序,以找出是否有数据下载。
我无法找到一种方法来取消下载处理程序,下面的应用程序提示我们保存一些垃圾HTML文件。
如何让downloadHandler
干净地退出?
library(shiny)
ui <- fluidPage(
downloadButton("btn", "Download")
)
server <- function(input, output, session) {
output$btn <- downloadHandler(
filename = function(){
"out.txt"
},
content = function(file){
# this example always fails.
if(TRUE){
print("Sorry, data not written.")
return(NULL)
} else {
writeLines(mtcars, "out.txt")
}
}
)
}
shinyApp(ui, server)
2条答案
按热度按时间ar7v8xwq1#
AFAIK,一旦按下按钮,就不可能取消下载。
下面是一种不使用
downloadHandler
下载文件的方法。它包括创建一个a
元素,一旦你按下一个按钮,你想要下载的文件将被转换为base64编码,在shinyjs::runjs
的帮助下,base64字符串将被赋予a
元素的href
属性,并触发对a
元素的点击。lymnna712#
我想有类似的东西,但在一个模块。诀窍是有一个隐藏的downloadbutton,使用JS(使用shinyjs)点击。
使用模块实现此功能的MWE如下所示