0

I am trying to download a datatable using modified data from Shiny to a csv. The app runs, I can upload my data, and the datatable is created, but when I try to download it, it does not download as a csv. There is a file that downloads but it has no file format listed and can't be opened. Is there something wrong with my download related code?

Download code in server.R

server <- function(input, output, session) {
  observe({
    file1=input$file1
    if (is.null(file1)){
      return(NULL)
    }
    df<-read.csv(file1$datapath, fileEncoding = "UTF-8-BOM")
    df$FAIL<-ifelse(df$OPERATION_STATUS %in% "FAIL",1,0)
setDT(a)
a[,group_i := as.Date(DATE) >= Sys.Date()-30]
dt<-(a[, .(
  "Overall Percent" = round(sum(OPERATION_STATUS == "FAIL") / .N * 100, 2),
  "30 Day Percent" = round(sum(OPERATION_STATUS[group_i] == "FAIL") / sum(group_i) * 100, 2),
  "Percent Change" = round((sum(OPERATION_STATUS[group_i] == "FAIL")/sum(group_i) / (sum(OPERATION_STATUS == "FAIL")/.N)*100), 2)),
  keyby = .("Area" = CRIT_CODE)])
output$tab<-DT::renderDataTable({
  datatable(dt, options=list(pagelength=30, lengthMenu=c(30,50)), rownames=FALSE, server=FALSE) %>% 
  formatStyle("Percent Change", color=styleInterval(100, c("black","red")))
})

output$dltab<-downloadHandler(
    filename=function(){
        paste("Fail Rate Summary-", Sys.Date(), ".csv", sep="")},
    content=function(file){
        write.csv(dt, file)
    }
)

Download Button Statement in ui.R

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File",
                accept=".csv"),
      width=3
    ),
    mainPanel(
      tabsetPanel(
        tabPanel(
          "Percent Fails", 
          DT::dataTableOutput("tab"), 
          downloadButton("dltab", "Download"))
  ))))

Sample Data

n <- 60
set.seed(21)
a <- data.frame(
  DATE = 
    rev(seq.Date(as.Date("2018-01-01"), as.Date("2018-06-15"), "days"))[1:n],
  OPERATION_STATUS = sample(c("PASS","FAIL"), n, replace = TRUE),
  CRIT_CODE = sample(c("A", "B", "C"), n, replace = TRUE)
)
  })  

}  
Lost
  • 331
  • 1
  • 12
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Have you started with something simple first like: https://shiny.rstudio.com/articles/download.html – MrFlick Jun 25 '18 at 17:45
  • @MrFlick I have updated the question, but from your comment, it seems there is nothing obviously wrong with the code? Or else you would have just been able to point it out without running it? – Lost Jun 25 '18 at 17:57
  • I'm confused, are `a` and `audit` supposed to be the same data.table? Also, is all your server code inside that first `observe()`? Or are there just syntax errors in your example code? – MrFlick Jun 25 '18 at 18:08
  • Yes to your first two questions. I didn't have the parentheses and brace at the end because I excluded code for a couple plots I generated and missed copying them – Lost Jun 25 '18 at 18:21

1 Answers1

1

When running the Shiny Application locally, via R studio, the program will not define the set file type. You will notice that once the application is deployed or run in a browser, the specified file format is in place, so this fixes the problem.

In order to download the .csv while running locally , simply specify the file name by including it in the name of the file, e.g.data.csv when prompted to save via the file explorer.

Note that when downloading locally, specifying the .csv in the file name within the code will not suffice, you have to type it in manually. Again this is fixed upon deploying or running in browser.

Chabo
  • 2,842
  • 3
  • 17
  • 32
  • Thank you for the explanation. It's unfortunate it works like that, but at least it works! – Lost Jun 25 '18 at 19:36
  • @Lost It is strange that this is still a problem since it has been a notable bug since ~2014. There may be some solutions to this if you desperately need this to work offline, look here https://stackoverflow.com/questions/25984138/shiny-app-downloadhandler-does-not-produce-a-file – Chabo Jun 25 '18 at 19:37