Home > OS >  Having Trouble Getting Download Handler in Shiny to Work
Having Trouble Getting Download Handler in Shiny to Work

Time:07-22

I am trying to build a shiny platform that can take in an input file, paste the output into a table on display, and then have a download button to download the results of the file back to your computer. I've tried numerous different ways, and it hasn't worked.

UI Code:

tabItem(tabName = "home",
           h2("Cancer Publications Compiler"),
           sidebarLayout(position = "left",
# File Upload
                         sidebarPanel(
                           radioButtons("data_t", "Select the Data Type Here",
                                        c(Excel = ",", "Text = "\t")),
                           fileInput("data_f", "Upload Your Data Here")),
# Download Input
                         mainPanel(width = 8,align = "center",
                                   div(style = "border:1px black solid;width:90%;font-size:10px;",tableOutput("out_chart")),
                                   downloadButton("downloadData", "Download")))
    ),

Server:

fileext = reactive({
    switch(input$data_t,
           "Excel" = "csv", "Text" = "txt")
  })
  ## Create Output for file selector ##
  data_file <- reactive({
    if(is.null(input$data_f)){return()}
    else{
      file_spec <- input$data_f
      aa <- read.table(file_spec$datapath, header = TRUE, sep = input$data_t)
      return(aa)
    }
  })
  
  # Connects the input and output to launch the data table
  
  ## Create Output for table from tool ##
  output$out_chart <- renderTable({
    if(is.null(input$data_f)){return()}
    else {
      data_file()
    }
  })

  output$donwloadData <- downloadHandler(
    filename = function(){
      paste("data-", fileext(), sep = ".")
    },
    
    content = function(file){
      sep <- switch(input$data_t, "Excel" = ",", "Text" = "\t")
      
      write.table(data_file(), file, sep = sep,
                  row.names = FALSE)
    })

Can anyone help me with a fix for this problem so that the download handler will work how I want it to?

UPDATE: I have edited the code and have updated it on here. My problem now is that when I click download to download the outputted table, I am getting a .htm download of a very low rendered and weird looking version of my webpage.

CodePudding user response:

You had some typos, and other issues. Try this

ui <- fluidPage(
  tabItem(tabName = "home",
          h2("Cancer Publications Compiler"),
          sidebarLayout(position = "left",
                        # File Upload
                        sidebarPanel(
                          radioButtons("data_t", "Select the Data Type Here", c("Excel" = "csv", "Text" = "txt")) ,
                           fileInput("data_f", "Upload Your Data Here")
                           ),
                         # Download Input
                         mainPanel(width = 8,align = "center",
                                   div(style = "border:1px black solid;width:90%;font-size:10px;",tableOutput("out_chart")),
                                   downloadButton("downloadData", "Download"))
                        )
    )
)

server<- function (input, output, session) {
  
  sep <- reactive({
    req(input$data_t)
    switch(input$data_t,
           "csv" = ",", "txt" = "\t")
  })
  
  ## Create Output for file selector ##
  data_file <- reactive({
    if(is.null(input$data_f)){return()}
    else{
      file_spec <- input$data_f
      aa <- read.table(file_spec$datapath, header = TRUE, sep = sep())
      return(aa)
    }
  })
  
  # Connects the input and output to launch the data table
  
  ## Create Output for table from tool ##
  output$out_chart <- renderTable({
    print(input$data_t)
    if(is.null(input$data_f)){return()}
    else {
      data_file()
    }
  })
  
  output$downloadData <- downloadHandler(
    filename = function(){
      paste("data-", input$data_t, sep = ".")
    },
    
    content = function(file){
      write.table(data_file(), file, sep = sep(), row.names = FALSE)
    }
  )
  
}

shinyApp(ui = ui, server = server) 
  • Related