Multiple progress bars

It is pretty easy to monitor the progress of a long loop in R using the original txtProgressBar function in the utils package.

It works like this:

mypb <- txtProgressBar()
m <- sapply(1:1000, function(x) {
  setTxtProgressBar(mypb, x/1000)

You could even get a GUI-type output using tkProgressBar from the tcltk package, or winProgressBar.

Or you could build your own. The solution lies in three lines of code:

plot.progress <- function(percent) {
	      plot(c(0,100), c(0,1), type='n', xlab='', ylab='', yaxt='n')
	      rect(0, 0.1, percent*100, 0.9, col='blue')
	      title(paste('Progress: ', round(percent*100,2), '%', sep=''))


What is more interesting is that you can now easily handle multiple progress bars at once, which can prove quite useful when you have embedded loops.

plot.progress <- function(...)	{
  vectOfBar <- c(...)*100
  numOfBar <- length(vectOfBar)
  plot(c(0,100), c(0,numOfBar), type='n', xlab='', ylab='', yaxt='n', mar=c(3,3,3,3))
  for(i in 1:numOfBar) {
    rect(0, 0.1+i-1, vectOfBar[i], 0.9+i-1, col=rainbow(numOfBar)[i])
    text(0.5, 0.5+i-1, paste('Status ', i, ': ', round(vectOfBar[i],2), '%', sep=''), adj=0)
plot.progress(0.7543, 0.6918, 0.3454)



blog comments powered by Disqus