Displaying Progress in a Loop or Function [R]

and how to create a screen recording GIF in Windows 10

JJ
JJ
Mar 16, 2016 · 3 min read

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.

print everything after loop is finished

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().

Add `flush.console()` to force print within loop

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.

Load `svMisc` to create pretty progress text

Example 3: Pretty Progress Text Bar

To get even fancier, set progress.bar = TRUE.

Load `svMisc` and set `progress.bar = TRUE` to create pretty progress bar

Screen Recording GIFs

Here’s a nice walk-through of how to use the Windows 10 built-in Xbox app to create an mp4 video and the free website I used for converting video to GIF:

Code-in-a-Gist

Human in a Machine World

Stories and reflections on understanding our imminent…

Human in a Machine World

Stories and reflections on understanding our imminent overlords. The theory and practice of using statistics and computing algorithms.

JJ

Written by

JJ

Thinker and Tinkerer. Figuring out a storyline. http://jilmun.github.io/

Human in a Machine World

Stories and reflections on understanding our imminent overlords. The theory and practice of using statistics and computing algorithms.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store