Progress indicator for R console

Sometimes you know you are in for a bit of a wait when you’re running some R code. However, it would be nice to get some idea of how long you’ll be waiting.

The txtProgressBar() command can help you here. The command allows you to set-up an indicator that displays in the console and shows your progress towards the “end point”.

txtProgressBar(min, max, style)

You set the starting and ending points via the min and max parameters, usually these will match up with the “counter” in a loop. The style parameter is a simple value, style = 3 shows a series of = characters marching towards the % completion (displayed at the end of a line in your console).

The setTxtProgressBar() command actually updates the progress indicator and displays in the console. When you are done you close()the progress bar to tidy up.

pb <- txtProgressBar(min = 0, max = 100, style = 3)
  for(i in 1:100) {
   Sys.sleep(0.1)
   setTxtProgressBar(pb, i)
   }
close(pb)

The first line sets up the progress indicator. The for() command sets a loop to run from 0 to 100. The loop in this example is trivial with the Sys.sleep() command simply making R “wait” 0.1 seconds. The 4th line updates the progress indicator. After the loop has ended the close() command “resets” the progress bar.


See more tips and tricks at DataAnalytics.org.uk

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s