Since I am a data junkie and bought my Fitbit Charge HR mainly because I wanted to collect and analyse data about myself, I was looking for ways to download the data to my computer. For most people the great stats overview in the app and in the online dashboard will be sufficient but some people like me will want to get more out of their data. In this post I’ll show you three ways how to access and analyse your Fitbit data in R. All three approaches will also work in other programming languages of course.
Code for this blog post can be found in my github repo.
For each of the three approaches I have created separate scripts, here is an overview:
- Data download: fitbit.data.R, plots_basic_data.R
- Web scraping with fitbitScraper: fitbitScraper.R
- Fitbit API: access_api.R
Fitbit provides a very basic data download for every user in their dashboard. Unfortunately it only provides summary statistics per day (total step counts, …). For a very first analysis it’s ok though. Since the output csv is not in a very nice format (several csv tables in one file with headers in between) I decided to only download Aktivitäten (activities) and Schlaf (sleep).
In fitbit.data.R I created 2 methods to read the data and get it into a useable format. In plots_basic_data.R I looked at my movement patterns and my sleeping behaviour. I thought it was funny to find out that my median sleeping time during the week was 7h33m and on the weekends 7h34m 😀 (over the past 3.5 months).
Another way to get Fitbit data is to use web scraping. So far I didn’t make very good experience with scraping data from sites where you have to login, but luckily there is an R package that can deal with Fitbit: fitbitScraper.
You just need your login data (I recommend you use environment variables for saving your password) and then you can start right away. In fitbitScraper.R I created a plot of my hourly activity for the past few months. It’s not very exciting (I have to dig into the data more) but it has a lot more details than the daily step counts.
Connecting to the Fitbit API was a bit hard but if you manage to do it you can get all the data (I assume) that they collect about you.
There are some helpful links that got me started:
There are some more links in my code file access_api.R. My main problem was that old examples on the internet were not correct anymore, but my code now is working if you want to download the data on your own.
The data returned by the API is JSON but the httr package gives you a list structure. It contains lots of general stats (awakeCount, …) and also a list minuteData with information about every minute that you were (almost) asleep. I noticed that each of these minutes had a value (1, 2 or 3) and couldn’t figure out what it meant. Looking at the API documentation helps in such cases, it says that there are 3 states: 1 (“asleep”), 2 (“awake”), or 3 (“really awake”).
Now that I figured out how to access the API I plan to create functions to get the data into a usable format and analyse myself a little bit more.