1

I'm building an app that operates regularly when nothing is given in a url query, however if a certain string is given there it should download the file immediately. The download works fine, but when it is run at start up it return a 'download.htm' file instead of the .csv. The reproducible example is not querying the url, but triggers in an observe:

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs()
  ,downloadButton("downloadData", "Download")
)

server <- function(input, output) {

  data <- mtcars

  observe({
    print("click MacClickFace")
    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)

How can you trigger the download at launch or are there some security things going on here?

1 Answer 1

1

Probably the downloadHandler is not ready. You can use a setTimeout(..., 0):

runjs("setTimeout(function(){document.getElementById('downloadData').click();},0);")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.