Analyzing Nike+ using R

A couple of weeks ago, I finally took up running seriously. To keep myself motivated -- especially on dark winter nights -- I bought the Nike+ iPod sports kit. The sports kit was developed by a partnership of Nike and Apple, and consists of a tiny accelerometer that you attach to one of your shoes (according to Nike it should go in a special built-in pocket of your special Nike+ compatible shoe, but there are of course plenty of other solutions), and a receiver you attach to your iPod. The accelerometer sends its data to your iPod, which in turn can tell you how far you ran and how far you still have to go. So far so good, but what you probably want to do is to analyze the data you have painfully gathered. And the way to do that, according to apple and nike, is to let iTunes send your data to the nikeplus website. This website is heavily flash-based, slow and only shows general trends -- it doesn’t let you touch your raw data. And that is exactly what I wanted. A couple of existing programs give you slightly better control over your data (e.g., Runner’s Log, TrailRunner), but none of them did cater to my needs.

To solve this I decided to write an R-script: how to better analyze data than by using R? Fortunately, the running data is stored in xml files on your iPod, which makes life easy. The script starts by copying new .xml files from a connected iPod (if there is one) to your hard disk, and parses the contents of the new files. Then, it displays the speed and pace over your last run, and graphs of speed, pace and distance of all your runs to date. For example:


Of course, as the script gives you access to the raw data, you can do with it what you like.


The last thing the current script does is importing a csv training schedule (e.g., 12/30/08,2,3.218688
- date/mile/km). It displays the distance you have to run each day, and shows what you have already managed:


If you would like to try it out, you can download my script here, if you have any questions about the script, please feel free to send me and email.

TextWrangler and R

R is an environment for statistical computing (see also ‘Data Analysts Captivated by R's Power’ in the New York Times) which I use on an almost daily basis. However, the included text editor is not all that great, and that is why I use TextWrangler to edit my R scripts. To get the most out of this combination, I searched the internet for syntax highlighting and for an apple script for executing my code directly from TextWrangler in R. It took me a while to find it, especially the script that only executes the selection in TextWrangler (as opposed to the complete file). This is the best I could find:
  • the syntax highlighting file. Save this file in ~/Library/Application Support/TextWrangler/Language Modules/ (make sure you keep the ‘.plist’ extension), and restart TextWrangler. Go to TextWrangler’s preferences -> Languages, and add a new suffix mapping (e.g., .r to R language).
  • an applescript to execute the selection in TextWrangler. Put this in ~/Library/Application Support/TextWrangler/Scripts/. You can assign a shortcut by going to the Window menu in TextWrangler, then choose Palettes -> Scripts and assign a shortcut, for instance cmd-return for the same behavior as the built-in R text editor. It either executes your current selection, or the line on which your cursor is. Note: for some versions of TextWrangler, you will have to rename the applescript from .txt to .scpt for TextWrangler to recognize the file (it turns out that you sometimes have to save the file with ‘script editor’, to get TextWrangler to recognize it, thanks Brian!).

Update September 18, 2009:
If you like the scripts above, you might also be interested in:
  • a script to automatically set your working directory in R to your TextWrangler document's directory.
  • a start-up script for R with a couple of useful function that should've been in R in the first place.