For long-running programs initiated from the command line, it can be helpful to 1) let the user know that the process is still active, and 2) provide a rough indication of the remaining runtime for the script in question. Fortunately, this is straightforward in R thanks to the builtin txtProgressBar function. This post demonstrates how to embed txtProgressBar in a command line application, and how to use txtProgressBars graphical counterparts, winProgressBar and tkProgressBar to implement progress bars outside of the command line interface.

txtProgressBar

txtProgressBar works well within the confines of an iteration scheme, most effectively in a setup with a predefined number of passes. In the following example, the call to Sys.sleep is used as a placeholder for work requiring some amount of time to complete. We also specify + to be used as progress bar character (defaults to =):

numIterations <- 100
progressBar   <- txtProgressBar(min=0, max=numIterations, char="+", style=3)
for (iter in seq(0, numIterations)) {
    Sys.sleep(.025)
    setTxtProgressBar(progressBar, iter)
}
close(progressBar)

The style argument can be set to 1, 2 or 3. 1 and 2 each show char without the percent complete to the right. They differ in that style 2 redraws the line each time, which is useful if other code might write to the console simultaneously.

tkProgressBar & winProgressBar

winProgressBar generates a graphical progress bar in a Windows dialouge. Usage is similiar to txtProgressBar, except winProgressBar takes title and label arguments to display on the GUI. You can also specify a width argument that determines the width of the dialouge (defaults to 300). In the following example, we update label at each iteration to display the percent complete:

numIterations <- 100
progressBar   <- winProgressBar(
                        title="My Windows Progress Bar", 
                        label="Progress Counter", 
                        min=0, 
                        max=numIterations,
                        width=300)

for (iter in seq(0, numIterations)) {
    Sys.sleep(.025)
    iterStatus <- paste(round(100*(iter/numIterations),0), "% Complete", sep="") 
    setWinProgressBar(progressBar, iter, label=iterStatus)
}
close(progressBar)

tkProgressBar is a cross-platform progress indicator that takes the same arguments as winProgressBar, but requires the tcltk package:

require(tcltk)

numIterations <- 100
progressBar   <- tkProgressBar(
                        title="My Tcl/Tk Progress Bar",
                        min=0, 
                        max=numIterations,
                        width=300)

for (iter in seq(0, numIterations)) {
    Sys.sleep(.025)
    iterStatus <- paste(round(100*(iter/numIterations),0), "% Complete", sep="") 
    setTkProgressBar(progressBar, iter, label=iterStatus)
}
close(progressBar)

Progress indicators are a great way to give peace of mind to the users of your program since it lets them know execution is proceeding as normal, and as demonstrated above, implementing txtProgressBar, winProgressBar or tkProgressBar can be worked into a long-running R script with very little effort. Happy coding!