download inconsistent according to data frame being downloaded in shinyapps.io

My app (uploaded to shiny apps.io) when downloading creates a -2.csv copy instead of overwriting the same-name previously created file. It is important I be able to keep the same name and overwrite the prior file.

To explore the problem I adapted the Shiny Help example I found online named "10_download" to my situation. I tested it with both R's data frame 'rock' and my data frame 'problem_df'. I wrote .csv files for both of these data frames and put them into my download folder.

The app does overwrite correctly into my download folder using 'rock'. It creates the -2 copy when using 'problem_df'; additionally, problem_df-2.csv has 4 extra bytes that problem_df.csv did not have. Additionally, I can then overwrite problem_df-2.csv just fine.

What is wrong with problem_df.csv that it does not get overwritten?

Here is my data:

"user","height","entry_date","hr_slept","min_exer","min_str","ate_med_mind","ate_processed","ate_fast","ate_sugar","ate_alcohol","mng_stress","social_contact","challenge_cog","new_learn","learn_what","weight","blood_sugar","systolic","diastolic","not_smoker","normal_hearing","not_depressed"
"f",69,2019-09-04,6,0,30,1,1,0,1,0,1,1,1,1,"Sql plus",0,0,0,0,0,0,0
"f",69,2019-09-03,5,30,0,0,0,0,0,0,0,0,0,0,"",0,0,0,0,0,0,0

I already have the code uploaded to shiny apps.io and here is the url:
https://cut-risk.shinyapps.io/adapted_10_download/

Here is the code:

library(shiny)

ui <- fluidPage(
  
  # App title ----
  titlePanel("Downloading Data"),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # Input: Choose dataset ----
      fileInput("fileIn", label = h3("Choose the file to read in (i.e. 'rock' or 'problem_df')")),
      
      # Button
      downloadButton("downloadData", "Download")
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      tableOutput("table")
      
    )
    
  )
)

server <- function(input, output) {
  
  # Reactive value for selected dataset ----
  datasetInput <- reactive({
    filename_selected = input$fileIn
    datIn = read.csv(filename_selected$datapath,stringsAsFactors = FALSE)
    datasetInput = list(datIn,filename_selected$name)
    
  })
  
  # Table of selected dataset ----
  output$table <- renderTable({
    filename_selected = input$fileIn
    validate(
      need(input$fileIn, "  ")
    )
      datasetInput()[[1]]
  })
  
  # Downloadable csv of selected dataset ----
  output$downloadData <- downloadHandler(
    filename = function() {
      datasetInput()[[2]]
    },
    content = function(file) {
      write.csv(datasetInput()[[1]], file, row.names = FALSE)
    }
  )
  
}
shinyApp(ui, server)

Thank you for any help!

I turned your app into a simple reprex, just to make sure I understood all the pieces:

library(shiny)

ui <- fluidPage(
  fileInput("file", "file to read in"),
  downloadButton("download", "Download"),
  tableOutput("table")
)

server <- function(input, output) {
  file_name <- reactive({
    req(input$file)
    input$file$name
  })
  
  file_contents <- reactive({
    req(input$file)
    read.csv(input$file$datapath,stringsAsFactors = FALSE)
  })

  output$download <- downloadHandler(
    filename = function() {
      file_name()
    },
    content = function(file) {
      write.csv(file_contents(), file, row.names = FALSE)
    }
  )
}
shinyApp(ui, server)

That seems to work fine for me. I wonder if the problem is actually coming from the browser — in Safari, if you download a file to a directory that already contains a file with that name, it automatically adds (1) etc to the file name. Maybe that's the problem that you're seeing?

Thank you for looking into this, Hadley.

Safari did not put the -1 extension on to the rock file, only my file. But you made an excellent point--so I tried the app on chrome and found the -1 extension on BOTH rock and my file.

I wonder if there is any write statement to put into my download handler that can force an overwrite.

Is it possible that your csv file is open in another app, e.g. Excel locks files when it opens them, sometimes this locking gets stuck. Can you open this file in another program?

Thanks Woodward. No, the file is not in use elsewhere. That was one of the theories I originally chased down, trying to troubleshoot this.

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