【问题标题】:How to stop processing until a file is selected when ShinyFileChoose processing is performed执行 ShinyFileChoose 处理时如何停止处理直到选择文件
【发布时间】:2021-02-12 02:44:34
【问题描述】:

我们目前正在开发一个闪亮的应用程序。 当我使用 ShinyFileChoose 读取文件时,我想将读取的文件存储在一个变量中并将其连接到后续处理。 因此,我想在使用 ShinyFileChoose 选择文件时停止该过程。 我该怎么办?

示例代码如下。

library ("shiny")
library ("shinyWidgets")

ui <-fluidPage (
  shinyFilesButton ('file','Reading file ...','choose file', FALSE),
      textOutput ("OUTPUT_TEXT")
)

server <-function (input, output, session) {
  
  observeEvent (input $ file, {
    
    volumes <-c ("Documents" = Sys.getenv ("HOME"))
    shinyFileChoose (input,'file', session = session, roots = volumes, filetypes = c ('','txt'))
    
    unlistfile <-unlist (input $ file)
    filename <-c (unlistfile [2] [1])
    
    output $ OUTPUT_TEXT <-renderText ({
      paste0 (volumes, "\\", filename)
    })
  })
}

shinyApp (ui = ui, server = server)

这里,文件路径和文件名通过paste0的过程输出到textOutput。 我想把这个进程换成read_csv()的进程,连接到读取文件的进程。 但是,由于使用 shinyFileChoose 不会停止处理,因此使用 NA 处理输入 $ 文件并发生错误。

如果有人知道对策,请告诉我。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    你可以试试:

    library(shiny)
    library(shinyWidgets)
    library(shinyFiles)
    
    ui <-fluidPage (
      shinyFilesButton ('file','Reading file ...','choose file', FALSE),
      tableOutput("OUTPUT_TABLE")
    )
    
    server <-function (input, output, session) {
      
      observeEvent (input$file, {
        volumes <- c("Documents" = Sys.getenv ("HOME"))
        shinyFileChoose (input,'file', session = session, roots = volumes, filetypes = c ('','txt'))
        if(length(input$file) <= 1) return({})
        output$OUTPUT_TABLE <- renderTable ({
          df <- read.csv(input$file$files[[1]][[3]])
          df
        })
      })
    }
    
    shinyApp (ui = ui, server = server)
    

    【讨论】:

    • 感谢您的 cmets 。但是发生了错误 「[[:]中的错误
    • 抱歉,您的评论似乎不完整。我在我系统上的几个文件上测试了这个,它没有任何错误。
    • 明白了。是版本问题吗?关闭一次没有问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2012-06-08
    • 1970-01-01
    • 2014-01-22
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多