print and cat are very similar, you can print out what you want to know with both, but with cat, you can use \r to print out the process rate in the same line, making the console more tidy. txtProgressBar is like a progress bar, but you can't use it to print out other messages you want to know (such as data value). The three kinds of code can be seen below.
在資料處理時,
當處理資料量特別大時,
會需要瞭解到資料處理的進度。
第一種是最簡單的使用print
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
max <- 200000 | |
for(i in 1:max){ | |
print(paste0(round(i/max*100,2),'%')) | |
} |
第二種是使用cat與 \r
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
max <- 200000 | |
for(i in 1:max){ | |
cat("\r",round(i/max*100,2), '% ') | |
} |
最後一種是使用txtProgressBar
會顯示進度條
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
max <- 200000 | |
##建立狀態列 | |
##style可以更改,個人認為3最好 | |
pb <- txtProgressBar(min = 0, max = max, style = 3) | |
for(i in 1:max){ | |
setTxtProgressBar(pb, i) | |
} | |
close(pb) |
第一種好處是一路印下來,
錯誤也可以用try catch去印,
紀錄運行的過程。
但是容易洗版。
第二種則只會印在同一行,
不會有洗版現象,
但如果要記錄問題,
就要用print以免被蓋掉。
第三種主要就是不會洗版且像個狀態列,
不過想印出運算結果等等我還是習慣用cat。
沒有留言:
張貼留言