'Download A File Generated Within An R Shiny App

I am currently trying to generate a Shiny App that at the press of a button generates and downloads data for a user. I have a user defined function that pulls data from some places, analyzes, and creates a .csv comprised of numerous tables.

The bottleneck I'm running into is successfully taking this .csv file comprised of multiple tables and allowing it to be easily downloaded. I have tried applying the solution presented here, but when I run the application script, all of the data is generated from the button but it doesn't bring up the download window as I'd hoped. Is there a way of using downloadHandler and link it to something other than a downloadButton or downloadLink function?

Current UI Function:

ui <- dashboardPage(
  dashboardHeader(title = "Download Center"),
  dashboardSidebar(
    sidebarMenu(id="mytabs",
                sidebarMenuOutput("menu")
    )
  ),
  dashboardBody(tabItems(
    
  tabItem(tabName = "DataHarah", h2("Download Your Data Here"),
                  textInput(inputId='startyear', label='What is the first year?'
                            ,value=2021, width = NULL, placeholder = "Inputs should be
                            a four digit year (e.g. 2022)."),
          textInput(inputId='endyear', label='What is the last year?'
                    ,value=2022, width = NULL, placeholder = "Inputs should be
                            a four digit year (e.g. 2022)."),
          actionButton("do", "Generate & Download Your Data!")      
                 ))
  ))

Current Server Function & Initialization:

observeEvent(input$do,
               {DataTalk(input$startyear,input$endyear) # function that generates a csv file
                                                        # containing multiple tables.
                 
                 output$downloadData <- downloadHandler(  #download handler that may not be connected properly.
                   filename = c('mycsv.csv'),
                   content = function(file){
                     file.copy('mycsv.csv',file)})})}
shinyApp(ui, server)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source