fitbit-load-in-R.ipynb
This notebooks demonstrates how to use the Fitbit data in R and how to make simple plots with the data.
This notebook requires you to use the Fitbit connection to get your activity data into Open Humans.
Given that the imported Fitbit data has a rather particular JSON format this notebook just gives an idea of how to extract individual data from the larger data set.
For a start let's load our required packages. To load the JSON we are using rjson
, which needs to be installed with install.packages
in the first step:
With that out of the way we can find the Fitbit data from our Open Humans account:
Now that we have our fitbit data stored in json_data
we can start to extract individual data types from it.
Let's focus on steps
for now. We create two empty vectors and from there we first iterate over the yearly_entry
in json_data$'tracker-steps'
. Each yearly_entry
has one main list, which is activities-tracker-steps
. Which is why we now can iterate over yearly_entry$'activities-tracker-steps'
and get the dateTime
and value
for each of these entry
.
This is not how you should do things in R
, but I couldn't find out a better way to extract that data. If you do: Please be in touch at bastian@openhumans.org!
Once that's done we can merge the dates
and steps
into a single data frame and convert the data types:
Now we can start our first plot: All of our daily steps
Let's now also compare the daily step counts between the years. For this we generate fake_date
s, which contain only the real day
and month
but are all normalized onto the same year
. With that we can now plot everything onto a single x-axis
by using the fake_date
for it and still keep the year
, using the variable of the same name:
This should give you an idea of how to read the Fitbit data. To see all the different data types that are in the fitbit data you can use the following:
And with that happy exploring!
This notebook requires you to use the Fitbit connection to get your activity data into Open Humans.
Given that the imported Fitbit data has a rather particular JSON format this notebook just gives an idea of how to extract individual data from the larger data set.
For a start let's load our required packages. To load the JSON we are using rjson
, which needs to be installed with install.packages
in the first step:
library('tidyverse')
library('httr')
library('jsonlite')
With that out of the way we can find the Fitbit data from our Open Humans account:
access_token <- Sys.getenv("OH_ACCESS_TOKEN")
url <- paste("https://www.openhumans.org/api/direct-sharing/project/exchange-member/?access_token=",access_token,sep="")
resp <- GET(url)
user <- content(resp, "parsed")
for (data_source in user$data){
if (data_source$source == "direct-sharing-102"){
fitbit_url <- data_source$download_url
}
}
temp <- tempfile()
download.file(fitbit_url,temp,method='wget')
json_data <- fromJSON(txt=temp)
Now that we have our fitbit data stored in json_data
we can start to extract individual data types from it.
Let's focus on steps
for now. We create two empty vectors and from there we first iterate over the yearly_entry
in json_data$'tracker-steps'
. Each yearly_entry
has one main list, which is activities-tracker-steps
. Which is why we now can iterate over yearly_entry$'activities-tracker-steps'
and get the dateTime
and value
for each of these entry
.
This is not how you should do things in R
, but I couldn't find out a better way to extract that data. If you do: Please be in touch at bastian@openhumans.org!
dates <- c()
steps <- c()
for (yearly_entry in json_data$'tracker-steps'){
dates <- append(dates,yearly_entry$'activities-tracker-steps'$dateTime)
steps <- append(steps,yearly_entry$'activities-tracker-steps'$value)
}
Once that's done we can merge the dates
and steps
into a single data frame and convert the data types:
df <- data.frame('date' = dates, 'steps' = steps)
df$steps <- as.numeric(as.character(df$steps))
df$date <- as.Date(as.character(df$date))
df$year <- as.character(format(df$date,"%Y"))
Now we can start our first plot: All of our daily steps
ggplot(subset(df,df$steps != 0),aes(date,steps,color=year)) + geom_point() + theme_minimal()
Let's now also compare the daily step counts between the years. For this we generate fake_date
s, which contain only the real day
and month
but are all normalized onto the same year
. With that we can now plot everything onto a single x-axis
by using the fake_date
for it and still keep the year
, using the variable of the same name:
df$date_wo_year <- as.character(format(as.Date(df$date),"%m-%d"))
df$fake_date <- as.Date(paste('2001',df$date_wo_year,sep='-'))
ggplot(subset(df,df$steps != 0),aes(fake_date,steps,color=year)) + geom_line() + stat_smooth() + facet_grid(year ~ .) + theme_minimal()
This should give you an idea of how to read the Fitbit data. To see all the different data types that are in the fitbit data you can use the following:
names(json_data)
And with that happy exploring!