library(tidyverse)
library(lubridate)
library(fitbitr)

Step 1: Register a Fitbit developer app

This health assessment utilizes the {fitbitr} R package (Kaye, 2021). The functions in the package are relatively straightforward, but getting access to the Fitbit API set up is a bit tricky.

The first thing to do is register a new Fitbit developer app at dev.fitbit.com. Here’s how I completed the form:



I got a warning from the dev website that the redirect URL needs to be an https, but it needs to be in the exact format I have above, because this is how R is set up to take you back to your R console. If you have to, complete the initial form with https://localhost:1410/ initially, then go back and edit it before you run your R script.

Once you submit your Fitbit dev app, you will be taken to a webpage that lists your OAuth 2.0 Client ID and Client Secret.

Step 2: Record your dev app credentials

Be sure to make a copy of your Fitbit dev app OAuth 2.0 Client ID and Client Secret. I find it easiest to securely store these in my local R user environment. You can open this file with the command usethis::edit_r_environ() and then retrieve stored variables with Sys.getenv().

In my R script below, note that you should save your credentials in the form FITBIT_CLIENT_ID = XXXXXX and FITBIT_CLIENT_SECRET = XXXXXX.

Once you have your credentials stored, close the .Renviron file and quit out of R entirely. When you restart R, your credentials are ready to go. You can check if things are stored correctly by running: Sys.getenv('FITBIT_CLIENT_ID') and Sys.getenv('FITBIT_CLIENT_SECRET').

Just be sure that you don’t share these credentials with anyone!

Step 3: Collect your Fitbit data

generate_token(client_id = Sys.getenv('FITBIT_CLIENT_ID'), 
               client_secret = Sys.getenv('FITBIT_CLIENT_SECRET')
)

Look at your daily steps

This block of code pulls data for 2 years of daily steps and plots them along with a green line marking March 11, 2020, the date the World Health Organization declared COVID-19 to be global pandemic.

start_date <- lubridate::today() - lubridate::years(2)
end_date <- lubridate::today()

steps_df <- steps(start_date, end_date)

ggplot(steps_df, aes(x=date, y=steps)) +
  geom_point() +
  geom_smooth() +
  geom_vline(aes(xintercept = as.Date("2020-03-11")),
             color = 'green') + 
  xlab(NULL) +
  ylab("Steps per Day") +
  ggtitle("Number of Daily Steps") +
  theme_bw()

Look at yesterday’s heart rate

This next block of code looks at my heart rate for every minute yesterday.

hr <- heart_rate_intraday(lubridate::today() - 1)

ggplot(hr, aes(x=time, y=heart_rate)) +
  geom_point() +
  geom_smooth() +
  xlab(NULL) +
  ylab("Beats per Minute") +
  ggtitle("Yesterday's Heart Rate") +
  theme_bw()

Look at resting heart rate

This next block of code looks at my resting heart rate across the past 2 months. Note that a Fitbit dev app’s rate limit is 150 API requests per hour for each user who has consented to share their data; and it resets at the top of each hour. This means that you are limited to retrieving the activity_summary() data for 150 days at a time. You could store this data, wait an hour, and the retrieve the next 150 days if you wanted to.

health_df = NULL

for (i in 0:20) {
  new_row =
    tibble(date = lubridate::today() - i,
           resting_hr = activity_summary(date)$resting_heart_rate,
           steps = activity_summary(date)$steps
           )
  health_df <-
    health_df %>%
    bind_rows(new_row)
}
ggplot(health_df, aes(x=date, y=resting_hr)) +
  geom_point() +
  geom_smooth() +
  xlab(NULL) +
  ylab("Beats per Minute while Resting") +
  ggtitle("Resting Heart Rate") +
  theme_bw()