Shiny Download Button Resized with reactive downloadHandler

Hello. I have a Shiny app with a download button. Everything works fine, but if I add a reactive object to the downloadHandler (example code below), the button icon disappears and the button collapses (no text visible) upon page load. I suspect it has something to do with the reactive object "result()" I have in the download handler but I am not sure how to work around this because the file name and content is only set after a button click which fires eventReactive and creates "result()".

output$download_gt_table  <- downloadHandler(
  
    # NOTE: result() is a list object created in a eventReactive() function. It has properties: down_file and 
    #tmp_file)
    filename = result()$down_file,
    content = function(file) {
      tmp_file <- result()$tmp_file
      file.copy(tmp_file, file)
    }
  )

Any help much appreciated!

Hi @fritzjooste. To assist in troubleshooting, can you please provide all of the shiny app code, or a smaller app that demonstrates the issue you’re seeing?

Hi @scottyd22 - many thanks for helping me with this. Below is a reduced example of my app which reproduces the error. When I run this app, the download button briefly flashes with the icon visible and the size correct, but then the icon disappears and the button collapses to a thin sliver.

library(tidyverse)
library(readxl)
library(shiny)
library(jsonlite)
library(waiter)
library(gt)
library(webshot2)
library(lubridate)
library(bslib)

tool_names <- c("A", "B", "C") 

ui <- page_sidebar(
  
  title = "Demo",
  
  sidebar = sidebar(width = 450,
                    
                    waiter::use_waiter(),
                    
                    card(
                      full_screen = FALSE,
                      card_header("Report Selection and Setup"),
                      selectInput("tool_name", "Report Tool to Run", 
                                  choices = tool_names, selected = "A",
                                  width = "80%"),
                      fileInput("setup_upload", NULL, buttonLabel = "Report Setup File", 
                                multiple = FALSE, accept = c(".json"), width = "90%"),
                      
                      actionButton("btn_go", "Generate Report", width = "40%")
                    ),
                    
                    
                    
  ),
  card(
    full_screen = FALSE,
    card_header("Table Report"),
    gt_output("tool_gt_table"),
    downloadButton("download_gt_table", label = "Download")
  ),
  
)


server <- function(input, output, session) {
  
  result <- eventReactive(input$btn_go, {
    
    result = list(down_file = "none", tmp_file = "none")
    return(result)
    
  })
  
  output$download_gt_table  <- downloadHandler(
    
    filename = result()$down_file,
    content = function(file) {
      tmp_file <- result()$tmp_file
      file.copy(tmp_file, file)
    }
  )
  
} 

# Run the application 
shinyApp(ui = ui, server = server)

Thanks again!

Thank you for providing the example! It looks like the issue stems from the eventReactive not being created until the button is clicked. Thus, nothing initially exists for result(), which is being passed to downloadHandler(). This can be corrected by adding ignoreNULL = F, as shown below.

result <- eventReactive(input$btn_go, {
    
    result = list(down_file = "none", tmp_file = "none")
    return(result)
    
  }, ignoreNULL = F)
1 Like

Thanks so much @scottyd22 !!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.