Accessing your Fitbit data

[This article was first published on r-bloggers – verenahaunschmid, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 your 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:

  1. Data download: fitbit.data.R, plots_basic_data.R
  2. Web scraping with fitbitScraper: fitbitScraper.R
  3. Fitbit API: access_api.R

Data download

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

Fitbit Data export.
Data download @Fitbit.
Fitbit csv file.
Fitbit csv file.

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

ggplot of my sleeping behaviour.
During the week (left) and on the weekend (right).

Web scraping

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.

Hourly Fitbit activity.
Hourly Fitbit activity.

Fitbit API

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

Fitbit sleep data part 1.
Fitbit sleep data part 1.
Fitbit sleep data part 2.
Fitbit sleep data part 2.

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.

The post Accessing your Fitbit data appeared first on verenahaunschmid.

To leave a comment for the author, please follow the link and comment on their blog: r-bloggers – verenahaunschmid.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)