Displaying Progress in a Loop or Function [R]
While I was waiting for my not-very-complicated setup but complicated gbm model to run (it’s still running), it occurred to me that I really appreciate seeing which iteration my modeling function is currently on, especially if the run-time is many hours. Partially this is to make sure that the program hasn’t frozen and is still running and that functions don’t get stuck in endless loops, but some bigger motivator might be that seeing the progress fulfills some human need of having control… At least I’m aware.
Whatever the reason, I like being shown progress. Many modeling and other functions don’t have a built-in progress indicator and saves all the output results for after the function completes. One of the simplest cases that can be used to see this is is the for loop.
Note: All the code used below can be found in the gist at the bottom of this article. The GIFs show base R screenshots because RStudio was taken up by the gbm run.
Example 1: Simple For Loop
The GIF below shows a simple loop that goes through the integers 0 to 101 with a print(i) command and short lag time between each iteration.
The output to the R console is buffered. This means that all the outputs are printed to the console at once after the loop is finished.
Example 2: Simple For Loop with flush.console
The R FAQs suggest as a solution to either change the R GUI buffering settings in the Misc menu (Ctrl-W) or to tell R explicitly to empty the buffer by adding the line flush.console().
This method can be very helpful for debugging. When writing or editing a function within a for loop, message(paste0(“some text: ”, somevar)) can also be used to print interim results.
Example 3: Pretty Progress Text
To get fancier, install the svMisc package and use the progress() function.
Example 3: Pretty Progress Text Bar
To get even fancier, set progress.bar = TRUE.