Dowload a file without using downloadButton (nor downloadHandler ?)

2,353 views
Skip to first unread message

Dominique Emmanuel

unread,
Sep 15, 2016, 8:51:35 AM9/15/16
to Shiny - Web Framework for R
Hi,

For some reasons, I would like a shiny application that allows the user to download a file but without using downloadButton.

In my application, the download should begins when an action on my_input is done :
## Somewhere in the server function
my_file <-  "c:/an_excel_file.xlsx" # a path to an Excel file
a_name <- "my_downloaded_file.xlsx" # name of the file when downloaded on the client side

observeEvent(input$my_input#
my_input do not refers to a downloadButton
, {
## ???? Some code using my_file and a_name and causing the start of downloading in the client side
})


How can I fill the ## ???? part ?
With something like session$registerDownload ?

Best regards,
Dominique






 

Dominique Emmanuel

unread,
Sep 15, 2016, 9:00:05 AM9/15/16
to Shiny - Web Framework for R
NB : my_file is a file path in the server side (maybe in the same directory than ui.R or server.R for instance)

Dominique Emmanuel

unread,
Sep 16, 2016, 3:24:13 PM9/16/16
to Shiny - Web Framework for R
I have found a (not so pretty) solution (but it works), so I share it :

1. First, in the UI part I create an invisible download link (I set label ="" to get it invisible => it allows to declare a downloadLink but not to see it):
downloadLink("downloadData",label="")

2. I also declare (in the UI part) this javascript code (inspired by this) :
tags$head(tags$script(HTML('
                           Shiny.addCustomMessageHandler("jsCode",
                           function(message) {
                           eval(message.value);
                           });'

)))

3. In the server part I create thise observer (partially inspired by this)
observeEvent(input$my_own_trigger, {
  output$downloadData<<-downloadHandler(filename = x$Nom,content = function(file)file.copy(file0,file) )
  jsinject <- "setTimeout(function(){window.open($('#downloadData').attr('href'))}, 100);"
  session$sendCustomMessage(type = 'jsCode', list(value = jsinject))    
})


So now I can produce a download in the client side with any trigger (input$my_own_trigger)

Danny Challis

unread,
Mar 23, 2017, 12:03:49 PM3/23/17
to Shiny - Web Framework for R
This worked quite well except that now Chrome is blocking the download as a pop-up. Policy doesn't allow adding exception. Did you find a way around this problem too?
Thanks!

Danny.

Joe Cheng

unread,
Mar 23, 2017, 12:25:26 PM3/23/17
to Danny Challis, Shiny - Web Framework for R
I don't know of a way around this; and if there was a workaround, I imagine the Chrome team would probably consider it a bug that needs to be fixed.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/4e92667b-9e1e-48b9-b6f4-dfa74b9e04ff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages