Multiple progress bars

February 20, 2012

(This article was first published on, and kindly contributed to R-bloggers)

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)


To leave a comment for the author, please follow the link and comment on their blog: offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)