For Loop Tracking (Windows Progress Bar)

August 20, 2014
By

[This article was first published on Analyst At Large » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I know that many people consider for-loops the devil, but every so often I get lazy and I use them in the office.  Sometimes it’s nice to keep track of how far through the loop R has gone.  A couple years back I stumbled upon a post where I learned that a Windows Progress Bar could be created:

ProgressBar

Unfortunately, I have no idea where I got this code snippet, so I can’t give the proper source.  Anyway, here is the code:

###### Initialize the Progress Bar
pb <- winProgressBar(title="Example progress bar", label="0% done", min=0, max=100, initial=0)
 
###### Modifying the Progress Bar (using setWinProgressBar function)
for(i in 1:100) {
	Sys.sleep(0.1) # slow down the code for illustration purposes
	info <- sprintf("%d%% done", round((i/100)*100))
	setWinProgressBar(pb, i/(100)*100, label=info)
}
 
###### Closing the Progress Bar
close(pb)

To leave a comment for the author, please follow the link and comment on their blog: Analyst At Large » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



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

Sponsors

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)