【问题标题】:R Shiny where to place withProgressR Shiny 在哪里放置进度
【发布时间】:2016-02-11 17:59:23
【问题描述】:

在我的 Shiny 页面上,有一个读取大日志文件的步骤,需要 25 秒才能加载。我想在用户单击按钮后显示进度条。否则他们可能会在等待时认为它不起作用。

 #ui.R
 #I have an actionButton to activate reading the log file
 actionButton("getLog","Get Log data")

 #server.R
   observeEvent(input$getLog, {
 nL = as.numeric(countLines("/script/cronlog.log"))
 Log = read.table("/script/cronlog.log",sep = ";",skip = nL-1000)
 LogFile = tail(Log,100)
 colnames(LogFile) = "cronlog"
 })

我试图使用 withProgress 但我不知道如何使用它来包装代码。我尝试过这样的事情:

  observeEvent(input$getLog, {
withProgress(message = 'Calculation in progress',
                              detail = 'This may take a while...', value = 0, {
                   for (i in 1:60) {
                     incProgress(1/60)
                     Sys.sleep(0.25)
                   }
                 })
nL = as.numeric(countLines("/script/cronlog.log"))
Log = read.table("/script/cronlog.log",sep = ";",skip = nL-1000)
LogFile = tail(Log,100)
colnames(LogFile) = "cronlog"
})

进度条确实出现了,但加载进度似乎在进度条之后运行,这使得过程更长。我想我没有正确包装代码。

有什么建议吗?

提前谢谢你!

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    如果您应用的操作不是离散的,withProgress 对您没有多大帮助。您可以在各个语句之间增加进度条:

    nL = as.numeric(countLines("/script/cronlog.log"))
    incProgress(1/4)
    log = read.table("/script/cronlog.log",sep = ";",skip = nL-1000)
    incProgress(1/4)
    ...
    

    但我怀疑这会产生巨大的影响。另一种方法是将输入文件拆分为多个块,并在每个文件之后读取这些独立递增的计数器。

    在实践中,我会考虑删除以下部分:

    nL = as.numeric(countLines("/script/cronlog.log"))
    

    并使用标准系统实用程序仅通过管道传输所需的数据:

    read.table(pipe("tail -n 1000 /script/cronlog.log"), sep = ";")
    

    或直接与data.table::fread:

    fread("tail -n 1000 /script/cronlog.log", sep = ";")
    

    【讨论】:

      猜你喜欢
      • 2011-06-29
      • 2018-03-27
      • 2011-02-15
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多