Reload the same file

I follow a simple example of downloading files from a brilliant gallery, but with a slight modification. I need to modify csv files locally and see the changes reflected in the user interface. However, I believe that this is not possible if we do not interrogate any changes in the source.

Therefore, I simplify the problem by allowing file reloading. But this also does not happen in Brilliant. After downloading the file "file1.csv" I can not download the same file again. I need to upload another file "file2.csv" and then again the original file "file1.csv".

This takes a lot of time, and I wonder if anyone has encountered such a problem and may have found a solution for it.

+4
source share
2 answers

Adding jquery to clear the fileInput value does the trick.

...
fileInput("csvInput", "Upload CSV file", multiple = TRUE,
              accept=c('text/csv',
                       'text/comma-separated-values,text/plain',
                       '.csv')),
tags$script('$( "#csvInput" ).on( "click", function() { this.value = null; });'),
...
+3
source

In fact, this is one of the drawbacks of the Shiny module fileInput: it remains stationary if the same file was selected again and there is no built-in parameter force-upload.


To ensure reloading, the basic idea is:

  • After each upload, save the downloaded data to the repository reactiveValues.
  • Apply new fileInputto replace the original
  • Show message with a successful download in a new one fileInputfor better user convenience.

In ui.Rusing

uiOutput('fileImport')

In server.R:

# reactive storage
v <- reactiveValues()
# fileInput index
v$fileInputIndex <- 1

updateFileInput <- function (name = NULL){
  # update output with a new fileInput module
  output$fileImport <- renderUI({
    index <- isolate(v$fileInputIndex)
    result <- div()
    result <- tagAppendChild(
      result,
      fileInput(paste0('file', index), 'Choose CSV File',accept=c('text/csv','text/comma-separated-values,text/plain','.csv'))
    )
    # show a message of successful uploading
    if(!is.null(name)){
      result <- tagAppendChild(
        result, 
        div(name," upload complete")
      )
    }
    result

  })
}

dataInputRaw <- reactive({
  # equals to `input$file1` when initialized
  inFile <- input[[paste0('file', v$fileInputIndex)]]

  # TICKY PART:
  # 1. If initialized, `inFile` and `v$data` are both `NULL`
  # 2. After each uploading, new `fileInput` is applied and
  #    we want to keep previous updated data.
  #    It also prevent recursive creation of new `fileInput`s.
  if (is.null(inFile)){
    return(v$data)
  }

  # load as data frame
  v$data <- data.frame(read.csv(inFile$datapath))

  # if file successfuly uploaded, increate the index
  # then upload `fileInput` with successful message
  if (!is.null(v$data)){
    v$fileInputIndex <- v$fileInputIndex + 1
    updateFileInput(name = inFile$name)
  }

  # return data
  v$data
})

# init 
updateFileInput()

I tested this snippet and it works.

+1
source

Source: https://habr.com/ru/post/1621511/


All Articles