Accessing FitBit data in R

September 9, 2013
By

(This article was first published on Bioinfpharmatics » R, and kindly contributed to R-bloggers)

I caught a pretty amazing episode of Horizon (the BBC’s in depth science program in the UK) a while back called “The future of medicine is apps“. The programme explored the health benefits of giving people data about their body, health and lifestyle. The more extreme examples included the tracking of the England rugby team during training which allows the coaches to predict injury/flu before the player’s are aware of it, and the professor that monitored the level of every metabolite in his blood every day and was able to diagnose himself with Chron’s disease prior to any symptoms. At the more practical level were the people who simply track their activity levels each day. The theory goes that if you are aware via a direct data feed of what you are doing (or not doing I suppose) then you are able to make changes to your lifestyle for the better. Being a just a bit of a geek I was inspired to get myself an activity tracker and see what it was to collect some data on myself.

I settled on a FitBit Flex, which is essentially a pedometer that you wear on your wrist and which tracks activity (steps) as well as sleep patterns. I have to say it works really well and I am mightily addicted to trying to meet my activity goal each day – currently set to the default of 10,000 steps. FitBit provide a fairly slick website to display all the data you collect but, unfortunately, if you want to download the data and do any kind of analysis yourself you have to pay a pretty exorbitant subscription fee. Luckily, you can get at your data via their API if you have the know-how so I decided have a go in R.

First off you have to register an “app” with FitBit (mine is called StepTrack!) in order to get the credentials needed for authentication. I used the httr package for the OAuth authentication and data retrieval.

library(httr)

token_url = "http://api.fitbit.com/oauth/request_token"
access_url = "http://api.fitbit.com/oauth/access_token"
auth_url = "http://www.fitbit.com/oauth/authorize"
key = "my_key"
secret = "my_secret"

fbr = oauth_app('StepTrack',key,secret)
fitbit = oauth_endpoint(token_url,auth_url,access_url)
token = oauth1.0_token(fitbit,fbr)
sig = sign_oauth1.0(fbr, token=token$oauth_token, token_secret=token$oauth_token_secret)

# get all step data from my first day of use to the current date:
steps = GET("http://api.fitbit.com/1/user/-/activities/steps/date/2013-08-24/today.json",sig)

The data is returned as json, which can then be plotted to your hearts content. In the plot you can see a five day gap – I went on holiday and forgot the charger!

> steps
Response [http://api.fitbit.com/1/user/-/activities/steps/date/2013-08-24/today.json]
Status: 200
Content-type: application/json;charset=UTF-8
{"activities-steps":[{"dateTime":"2013-08-24","value":"5455"},{"dateTime":"2013-08-25","value":"11822"},{"dateTime":"2013-08-26","value":"11692"},{"dateTime":"2013-08-27","value":"17028"},{"dateTime":"2013-08-28","value":"10225"},{"dateTime":"2013-08-29","value":"8632"},{"dateTime":"2013-08-30","value":"9920"},{"dateTime":"2013-08-31","value":"9321"},{"dateTime":"2013-09-01","value":"13581"},{"dateTime":"2013-09-02","value":"7465"},{"dateTime":"2013-09-03","value":"0"},{"dateTime":"2013-09-04","value":"0"},{"dateTime":"2013-09-05","value":"0"},{"dateTime":"2013-09-06","value":"0"},{"dateTime":"2013-09-07","value":"335"},{"dateTime":"2013-09-08","value":"9239"},{"dateTime":"2013-09-09","value":"17059"}]}

step_plot

Admittedly I am struggling to come up with ideas of what to do with the data that FitBit doesn’t provide already through their website. But, its the principal of the thing – I should be able to get at my data and now I can. For all of the data shown in the above I was on holiday and in general much more active than when i’m plonked in my desk at work. It will be interesting to see what my daily step count is on a normal working day and whether knowing this will push me on to go for a run at lunch time or take the very long route to the sandwich shop. Being a very competitive person, I suspect it will.


To leave a comment for the author, please follow the link and comment on his blog: Bioinfpharmatics » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.