【问题标题】:Re-upload same file shiny R重新上传相同的文件闪亮的R
【发布时间】:2016-03-30 05:41:26
【问题描述】:

我正在关注来自闪亮画廊的simple file upload example,但稍作修改。我需要在本地修改 csv 文件,并查看 UI 上反映的更改。但是,我认为除非我们轮询源中的任何更改,否则这是不可能的。

因此,我通过允许重新上传文件来简化问题。但是,这也不会在 Shiny 中发生。上传文件“file1.csv”后,我无法再次上传相同的文件。我必须上传不同的文件“file2.csv”,然后再次上传原始文件“file1.csv”。

这只是耗时,我想知道是否有人遇到过这样的问题,并且可能找到了解决方案。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    添加一个 jquery 来清除 fileInput 的值就可以了。

    ...
    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; });'),
    ...
    

    【讨论】:

      【解决方案2】:

      其实这是Shiny的fileInput模块的缺点之一:如果再次选择相同的文件,它会保持静止,并且没有内置force-upload选项。


      强制重新上传,基本思路是:

      1. 每次上传后,将上传的数据保存在reactiveValues作为存储空间。
      2. 申请一个新的fileInput 替换原来的@
      3. 在新的fileInput 下显示成功上传消息,以获得更好的用户体验。

      ui.R,使用

      uiOutput('fileImport')
      

      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()
      

      我已经测试过这个 sn-p 并且它可以工作。

      【讨论】:

      • 感谢您的回答。但我无法使用 reactiveValues()。我正在使用观察者({})函数,它与反应性({})混合得不好
      猜你喜欢
      • 2015-05-16
      • 2013-06-26
      • 1970-01-01
      • 2015-10-14
      • 2020-10-18
      • 2015-05-15
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多