2016-02-11 43 views
2

在我的Shiny页面上,有一个步骤用于读取大型日志文件,该文件需要25s才能加载。我想在用户点击一个按钮后显示一个进度条。否则,他们可能会认为它在等待时不工作。R Shiny放置在哪里withProgress

#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" 
}) 

进度条没有出现,但它似乎是加载进度的进度条,这使得该方法甚至更长的时间之后运行。我想我没有正确包装代码。

有什么建议吗?

预先感谢您!

回答

2

如果您申请的操作不是离散的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 = ";")