Unable to download data from Shiny app hosted on Shinyapps.io

Hi, I have built a Shiny app that allows users to upload a .txt file, filter for key terms and then download the returned dataset. When hosting locally it works. However, when I deployed it to shiny apps.io the download function no longer works. Everything else (e.g the data upload + wrangling step) do work. Nothing comes up as an error in the code log. The file it downloads is called 'downloadData.html' and simply says 'Please wait. Loading'. When I run and download locally it returns a .txt file (which is strange as the function is write.csv). My code is

library(tidyverse)
library(shiny)
library(rsconnect)






ui <- fluidPage(
  titlePanel("Download data"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose .txt file",
                multiple = F,
                accept = c(".txt")),
      textInput('energy_co', 'Name of energy company'),
      textInput('asset', 'name of Asset Manager'),
      downloadButton("downloadData", "Download")
    ),
    
    mainPanel(
      tableOutput("table")
    )
    
  )
)


options(shiny.maxRequestSize=30*1024^2)

server <- function(input, output, session) {
  
  company_data <- reactive({
    req(input$file1,input$asset,input$energy_co)
    
    data <- read_lines(input$file1$datapath)
    text_df <- as_data_frame(data)
    
    company_data <- text_df %>% 
      filter(str_detect(value, input$asset)) %>%   
      filter(str_detect(value, input$energy_co)) %>% 
      distinct(.)
    company_data
  })
  
  
  output$table <- renderTable({  
    
    company_data()
    
  })
  
  output$downloadData <- downloadHandler(
    filename = function() {
      paste(company_data(), ".csv", sep = "")
    },
    content = function(file) {
      shiny::withProgress(
        message = paste0("Downloading", company_data(), " Data"),
        value = 0,
        {
          shiny::incProgress(1/10)
          Sys.sleep(1)
          shiny::incProgress(5/10)
      write.csv(company_data(), file, row.names = FALSE)
    }
  )
    }
  )
  
}


shinyApp(ui, server)

It seems to be an issue with the way you are using the company_data reactive in different places.
Company_data seems to be a data frame and you are using this to set the filename and message.
Try the code below and you probably see what's going wrong:
paste(mtcars, ".csv", sep = "")

That didn't work unfortunately - I substituted the original to:

  output$downloadData <- downloadHandler(
    filename = function() {
      paste("mydata", Sys.Date(), ".csv", sep = "")
    },
    content = function(file) {
      shiny::withProgress(
        message = paste0("Downloading", company_data(), " Data"),
        value = 0,
        {
          shiny::incProgress(1/10)
          Sys.sleep(1)
          shiny::incProgress(5/10)
     write.csv(company_data(), file, row.names = FALSE)
    }
  )
    }
  )

But the same issue arises (and still works when hosted locally).

hi @alex_stephenson you will need to update the message as well since you are pasting a string with a data.frame.

If it still not works, could you add the file you are using in the upload?

Hi @ginberg, thanks for your help. Unfortunately changing the message to not include a data frame has not solved it and there is no way to share .txt files on the forum (as far as I'm aware). The app should work, though, with any .txt file that is saved with UTF-8 encoding. I have managed to work out where the issue might be occurring though. When the query just returns one result (e.g. a data frame with one string) the app works fine. It's when the query returns more than one that it does not work.

If you'd like I can upload an appropriate .txt file to a file sharing site.

Thanks again.

... you have omitted to say what change you attempted...

@nirgrahamuk thanks for pointing out.Have edited.

For the purpose of debugging, try changing

 company_data <- reactive({
    req(input$file1,input$asset,input$energy_co)
    
    data <- read_lines(input$file1$datapath)
    text_df <- as_data_frame(data)
    
    company_data <- text_df %>% 
      filter(str_detect(value, input$asset)) %>%   
      filter(str_detect(value, input$energy_co)) %>% 
      distinct(.)
    company_data
  })

to

 company_data <- reactive({iris})

@nirgrahamuk Yup the download here works as expected.

and if you put a browser() before the write.csv part of the downloadHandler and snoop on company_data(),
does it look fine ? what does it look like ?

I don't know what has changed since @ginberg's suggested edits but after removing the iris debugging example and substituting the old code it has begun to work. Thanks for your help. The final code was:

library(tidyverse)
library(shiny)
library(rsconnect)

ui <- fluidPage(
  titlePanel("Download data"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose .txt file",
                multiple = F,
                accept = c(".txt")),
      textInput('energy_co', 'Name of energy company'),
      textInput('asset', 'name of Asset Manager'),
      downloadButton("downloadData", "Download")
    ),
    
    mainPanel(
      tableOutput("table")
    )
    
  )
)


options(shiny.maxRequestSize=30*1024^2)

server <- function(input, output, session) {
  
  #company_data <- reactive({iris})
  
  company_data <- reactive({
    
   req(input$file1,input$asset,input$energy_co)
    data <- read_lines(input$file1$datapath)
    text_df <- as_data_frame(data)
    
    company_data <- text_df %>% 
      filter(str_detect(value, input$asset)) %>%   
      filter(str_detect(value, input$energy_co)) %>% 
      distinct(.)
    company_data
  })
  
  
  output$table <- renderTable({  
    
    company_data()
    
  })
  
  output$downloadData <- downloadHandler(
    filename = function() {
      paste("mydata", Sys.Date(), ".csv", sep = "")
    },
    content = function(file) {
      shiny::withProgress(
        message = paste0("Downloading"),
        value = 0,
        {
          shiny::incProgress(1/10)
          Sys.sleep(1)
          shiny::incProgress(5/10)
     write.csv(company_data(), file, row.names = FALSE)
   #   write.table(company_data(), file, row.names = F, sep = "\t")
    }
  )
    }
  )
  
}


shinyApp(ui, server) 
1 Like

This topic was automatically closed 54 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.