R 語言添加進度條


1:轉載自:https://zhuanlan.zhihu.com/p/24747506

循環中添加進度條,在用R語言做數據分析處理的過程中,我們經常會碰到一些數據量比較大進而導致循環執行好久的情況。等待的過程太煎熬了,最關鍵的是我們不知道現在已經完成了多少進度,從而決定是否停止重新修改代碼。

library(tcltk)  
u <- 1:2000  
  
#開啟進度條  
  
pb <- tkProgressBar("進度","已完成 %", 0, 100)  
  
for(i in u) {  
   info<- sprintf("已完成 %d%%", round(i*100/length(u)))  
   setTkProgressBar(pb, i*100/length(u), sprintf("進度 (%s)", info),info)  
}     

#關閉進度條  
close(pb)  

  

目前,還有一個新的包,叫做tcltk2,也可以是實現上述的功能,具體測試代碼如下。可以更詳細的設置進度欄的大小等屬性值:

 

library(tcltk)
library(tcltk2)

root <- tktoplevel()

l1 <- tk2label(root)
pb1 <- tk2progress(root, length = 300)
tkconfigure(pb1, value = 0, maximum = 9)

tkgrid(l1, row = 0)
tkgrid(pb1, row = 1)

for (index in 1:10){
  tkconfigure(l1, text = paste("Index", index))
  tkconfigure(pb1, value = index - 1)
  Sys.sleep(1)
}

  

2:sapply、lapply等添加 使用軟件包 pbapply      https://github.com/psolymos/pbapply

A lightweight package that adds progress bar to vectorized R functions (*apply). The implementation can easily be added to functions where showing the progress is useful (e.g. bootstrap). The type and style of the progress bar (with percentages or remaining time) can be set through options. The package supports snow-type clusters and multicore-type forking (see overview here).

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM