Where am I? Or, adding a counter to a running R process

A quick post in response to a question that came up on Facebook via Liz Freedman and Nic Campione: how do you add a counter/ticker to a running process in R? This is something that can be really helpful if you’re still in the process of trying to debug some code, as a process that involves thousands of iterations may be either stuck and broken, or it could be just taking its sweet time to finish, and without any output during the iterations, you can’t know which it’s doing. So, in one line, here’s a simple way of adding a counter to a for loop (although while and other style of loops follow the same logic):

for (i in 1:1000) {
  if (i%%100==0) print(paste("This is iteration number ", i))
  #all your complex code can go down here, whatever it is

In this case, I just added a rule that printed the iteration number every 100 loops (the code can be run directly in a terminal if you want to see what I mean). The two percent signs right beside each other are the modulo operation, so that when the remainder of the iteration is zero (that is, every 100 loops), it prints out a message. The number can be changed easily so that it is every 1000 instead of 100, and you can also use different rules, but I find that this is a relatively quick and easy way to add a counter.

As for making the whole process run faster, using something like the foreach package (and associated packages like doMC or doParallel) can significantly speed things up if you have a multicore processor on your computer.

2 responses to “Where am I? Or, adding a counter to a running R process

  1. Thank you Matt! These handy little tidbits can be so hard to track down (really, Google failed me), but they are so very useful!

  2. I hope you don’t mind me contacting you from the blue like this, but I came across your blog online quite by accident and was quite struck by our common interests. Let me introduce myself a little so you can see I’m not just some internet based maniac. My name is David Clark and I am a first year PhD student at the University of Essex, England. I am studying the macroecology (and biogeography) of microbes, something which we know little about considering their relative importance in nature. Within this, I am analysing large volumes of metadata and will be using R quite a considerable amount. At the moment I am just starting out learning how to use it, I must say I’m already staggered by the possibilities it presents.
    The other shared interest is that I am also an amateur palaeontologist, I have collected fossils from a coastal location nearby called Walton on the Naze, after 10 or so years of collecting I now have a sizeable collection of various taxa.
    Anyway, I shall continue to read your blog with interest and just thought that I’d reach out as I’m sure I could learn a lot from you and your research.

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 )

Facebook photo

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

Connecting to %s