Return to site

Download Fitbit Data in R

This post is written for rstudio beginners who have little to no experience working within this environment. Once you have your Fitbit key and secret, then you should be able to copy and paste this code directly into rstudio, edit the basedate and enddate (see below), and then have your data come out as a csv file. As shown below (see resources), I did not create this wonderful R package. Instead, I have edited and combined existing code to make this process easier for those that are just starting to access their data.

This post will be updated soon with pictures to show how to create a personal Fitbit API account, which allows you to get a Fitbit key and secret.

---
title: "Fitbit Interday"
author: "Benjamin W Nelson"
date: "1/6/2018"
---

##Resources:
This markdown was created by referencing these resources.
Logon to https://dev.fitbit.com/login

https://dev.fitbit.com/build/reference/web-api/activity/
https://github.com/teramonagi/fitbitr

```{r}
##Install fitbitr package
devtools::install_github("teramonagi/fitbitr")
devtools::install_github("teramonagi/fitbitr@issues-34")
```

#Steps to Download Interday Fitbit Data

####Load libraries
```{r}
library(fitbitr); library(ggplot2)
```

####Set the variables
```{r}
# As a global variable
FITBIT_KEY <- "Enter Key"
FITBIT_SECRET <- "Enter Secret"
# If you want, Default: "http://localhost:1410/"
FITBIT_CALLBACK <- "http://localhost:1410/"
```

####Get Token
```{r}
token <- fitbitr::oauth_token(language="en_US") #Set timezone
```

####Get Device Information
```{r}
get_devices(token)
```


#### Note on Variables:
-Base date: The range start date, in the format yyyy-MM-dd or today.
-End date: The end date of the range. You can put in "today" to pull data up to the most recent day of data
-Date: The end date of the period specified in the format yyyy-MM-dd or today.
-Period: The range for which data will be returned. Options are 1d, 7d, 30d, 1w, 1m, 3m, 6m, 1y


##ACTIVITY

####Get Heart Rate
```{r}
hr <- get_activity_time_series(token, "restingHeartRate", base_date="date you first started using fitbit", end_date="today")
hr
colnames(hr) <- c("date","resting.heart.rate") #This renames the columns
hr
```

####Get Steps
```{r}
steps <- get_activity_time_series(token, "steps", base_date="date you first started using fitbit", end_date="today")
steps
colnames(steps) <- c("date","steps") #This renames the columns
steps
```

####Get Distance
```{r}
distance <- get_activity_time_series(token, "distance", base_date="date you first started using fitbit", end_date="today")
distance
colnames(distance) <- c("date","distance") #This renames the columns
distance
```

####Get Floors
```{r}
floors <- get_activity_time_series(token, "floors", base_date="date you first started using fitbit", end_date="today")
floors
colnames(floors) <- c("date","floors") #This renames the columns
floors
```

####Get Active Calories
```{r}
calories <- get_activity_time_series(token, "calories", base_date="date you first started using fitbit", end_date="today")
calories
colnames(calories) <- c("date","calories.burned") #This renames the columns
calories
```

##SLEEP

####Get Sleep- Time in Bed
```{r}
timeinbed <- get_sleep_time_series(token, "timeInBed", base_date="date you first started using fitbit", end_date="today")
timeinbed
colnames(timeinbed) <- c("date","time.in.bed") #This renames the columns
timeinbed
```

####Get Sleep- Minutes Asleep
```{r}
sleep.minutes.asleep <- get_sleep_time_series(token, "minutesAsleep", base_date="date you first started using fitbit", end_date="today")
sleep.minutes.asleep
colnames(sleep.minutes.asleep) <- c("date","sleep.duration") #This renames the columns
sleep.minutes.asleep
#This is the same as Sleep Duration when you download from fitbit
```

####Get Sleep- Minutes Awake
```{r}
sleep.minutes.awake <- get_sleep_time_series(token, "date you first started using fitbit", base_date="2016-02-10", end_date="today")
sleep.minutes.awake
colnames(sleep.minutes.awake) <- c("date","minutes.awake") #This renames the columns
sleep.minutes.awake
```

####Get Sleep- Efficency
```{r}
sleep.efficiency <- get_sleep_time_series(token, "efficiency", base_date="date you first started using fitbit", end_date="today")
sleep.efficiency
colnames(sleep.efficiency) <- c("date","sleep.efficiency") #This renames the columns
sleep.efficiency
```

####Get Sleep- Minutes to Fall Asleep
```{r}
time.to.sleep <- get_sleep_time_series(token, "minutesToFallAsleep", base_date="date you first started using fitbit" end_date="today")
time.to.sleep
colnames(time.to.sleep) <- c("date","sleep.onset.latency") #This renames the columns
time.to.sleep
#This is usually 0 for auto logs
```

####Get Sleep- Number of Awakenings
```{r}
sleep.number.awakenings <- get_sleep_time_series(token, "awakeningsCount", base_date="date you first started using fitbit", end_date="today")
sleep.number.awakenings
colnames(sleep.number.awakenings) <- c("date","number.of.awakenings") #This renames the columns
sleep.number.awakenings
#This is different from what is downloaded from fitbit
```

##BODY

####Get Body Composition- Weight
```{r}
weight <- get_body_time_series(token, "weight", base_date="date you first started using fitbit" end_date="today")
weight
colnames(weight) <- c("date","weight") #This renames the columns
weight
```

####Get Body Composition- BMI
```{r}
bmi <- get_body_time_series(token, "bmi", base_date="date you first started using fitbit", end_date="today")
bmi
colnames(bmi) <- c("date","bmi") #This renames the columns
bmi
```

####Get Body Composition- Fat
```{r}
fat <- get_body_time_series(token, "fat", base_date="date you first started using fitbit" end_date="today")
fat
colnames(fat) <- c("date","body.fat.percentage") #This renames the columns
fat
```

####Merge Datasets
```{r}
library(tidyverse)

fitbit.interday <- plyr::join_all(list(hr, steps, distance, floors, calories, timeinbed, time.to.sleep, sleep.minutes.asleep, sleep.minutes.awake, sleep.number.awakenings, sleep.efficiency, weight, bmi, fat))

```

####Save Dataset
```{r}
write.csv(fitbit.interday, "fitbit.interday.csv")
```

####Next Update
The next update will add sedentary, lightly, fairly, and very active minutes

All Posts
×

Almost done…

We just sent you an email. Please click the link in the email to confirm your subscription!

OKSubscriptions powered by Strikingly