2016-02-11 49 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 = ";")