Does
"document.getElementById('downloadData').click();"
also work with Safari 12.0?
I believe it should work on any modern broswer. Are you finding an error when you try?
My question refers to Paul's (slightly adapted) solution in How to stop download in downloadHandler
library(shiny)
ui <- fluidPage(
shinyjs::useShinyjs(),
actionButton("init", "Download", icon = icon("download")),
downloadButton("downloadData", "Download", style = "visibility: hidden;")
)
server <- function(input, output) {
# Our dataset
data <- mtcars
observeEvent(input$init, {
shinyjs::runjs("document.getElementById('downloadData').click();")
})
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(data, file)
}
)
}
shinyApp(ui, server)
My point is that Safari with the recently released version 12.0 (perhaps for safety reasons) no longer seems to support .click() when it leads to a download, while other browsers like chrome and firefox still do.
My workaround is
library(shiny)
ui <- fluidPage(
shinyjs::useShinyjs(),
actionButton("init", "Download", icon = icon("download")),
downloadButton("downloadData", "Download", style = "visibility: hidden;")
)
server <- function(input, output) {
# Our dataset
data <- mtcars
shinyjs::runjs("document.getElementById('init').addEventListener('click',function(){
setTimeout(function(){document.getElementById('downloadData').click();},800)
});")
observeEvent(input$init, {
shinyjs::runjs("document.getElementById('init').addEventListener('click',function(){
setTimeout(function(){document.getElementById('downloadData').click();},800)
});")
})
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(data, file)
}
)
}
shinyApp(ui, server)
1 Like