Showing posts with label fitbit. Show all posts
Showing posts with label fitbit. Show all posts

Thursday, March 26, 2015

fitbitScraper 0.1.2 Now on CRAN

I've updated the fitbitScraper to 0.1.2 with the help of a friend that gave me access to his fitbit account so I could work out the heart rate API endpoints (thanks Shane!). This is a package that enables you to read daily or intraday data from Fitbit into R for graphing and analysis.

Here's some examples:

I'll download all my steps for last Saturday, March 21, 2015, then make a graph similar to how it appears on the fitbit dashboard. As you can see, I spent some time playing basketball in the morning and pretty much took it easy the rest of the day. Note that the function name has changed to get_intraday_data() from get_15_min_data() in version 0.1.1.

# install.packages("fitbitScraper")  
library("fitbitScraper")
# just reading from file to hide pw and to make .Rmd document to work...
creds <- readLines("pw.txt")
cookie <- login(email=creds[1], password=creds[2]) 
df <- get_intraday_data(cookie, what="steps", date="2015-03-21")  
library("ggplot2")  
ggplot(df) + geom_bar(aes(x=time, y=steps, fill=steps), stat="identity") + 
             xlab("") +ylab("steps") + 
             theme(axis.ticks.x=element_blank(), 
                   panel.grid.major.x = element_blank(), 
                   panel.grid.minor.x = element_blank(), 
                   panel.grid.minor.y = element_blank(), 
                   panel.background=element_blank(), 
                   panel.grid.major.y=element_line(colour="gray", size=.1), 
                   legend.position="none") 

plot of chunk unnamed-chunk-1

Now, let's take a look at the heart rate data. This is Shane's data… I have no idea what he was doing on this day. Note that whereas intraday data for steps is given in 15 minute intervals, fitbit provides the heart rate data in 5 minute intervals.

# just reading from file to hide pw and to make .Rmd document to work...
creds <- readLines("pw2.txt")
cookie <- login(email=creds[1], password=creds[2]) 
df <- get_intraday_data(cookie, what="heart-rate", date="2015-03-20")
library("ggplot2")  
ggplot(df) + geom_bar(aes(x=time, y=`heart-rate`, fill=`heart-rate`,
                          colour=`heart-rate`), stat="identity") + 
             xlab("") +ylab("heart-rate") + 
             theme(axis.ticks.x=element_blank(), 
                   panel.grid.major.x = element_blank(), 
                   panel.grid.minor.x = element_blank(), 
                   panel.grid.minor.y = element_blank(), 
                   panel.background=element_blank(), 
                   panel.grid.major.y=element_line(colour="gray", size=.1), 
                   legend.position="none") 

plot of chunk unnamed-chunk-3

And finally, the daily heart rate data comes with three data points for each day. Zone1, zone2, and zone3 presumably represent the time in a given heart rate zone. I suspect they correspond to “fat burn zone”, “cardio zone”, and “peak zone” respectively. More information on fitbit heart rate zones is available here.

# just reading from file to hide pw and to make .Rmd document to work...
creds <- readLines("pw2.txt")
cookie <- login(email=creds[1], password=creds[2]) 
df <- get_daily_data(cookie, what="getTimeInHeartRateZonesPerDay", 
                     start_date="2015-02-17", end_date="2015-02-23")
library("reshape")
df <- melt(df, id="time", variable_name="zone_num")
library("ggplot2")  
ggplot(df) + geom_bar(aes(x=time, y=value, fill=zone_num), 
                      position=position_dodge(), stat="identity") + 
             xlab("Date") +ylab("Minutes in Zone") + 
             theme(axis.ticks.x=element_blank(), 
                   panel.grid.major.x = element_blank(), 
                   panel.grid.minor.x = element_blank(), 
                   panel.grid.minor.y = element_blank(), 
                   panel.background=element_blank(), 
                   panel.grid.major.y=element_line(colour="gray", size=.1)) 

plot of chunk unnamed-chunk-4

I'd love to work out the details for the GPS in the fitbit Surge. Unfortunately, I don't have one. If you are interested in working with me on integrating that data into the package, hit me up… corynissen@gmail.com

Thursday, January 22, 2015

R Package to Download Fitbit Data

Edit 4/8/2015: Updated to use newer functions…
Edit 3/26/2015: fitbitScraper 0.1.2 is now on CRAN with new heart rate data capabilities.
Edit 2/2/2015: fitbitScraper 0.1.1 is now on CRAN

Fitbit is a device that tracks your daily activity. It's basically a pedometer, but it does a little more. It has an altimeter, so it can count flight of stairs climbed. It can detect your sleeping activity and give you a read on how often you are tossing and turning. Any how, I received a Fitbit One for Christmas and have been unexpectedly into this thing. I absolutely did not expect to be motivated by monitoring my fitness activity. It's been fun.

The problem is that Fitbit doesn't allow you to export your data under their “free” data tracking tier. It costs $50 a month to upgrade to “premium” to allow for data export. It's my data, I should be able to get it. So, I made an R package to do just that.

fitbitScraper is my new R package, hosted on Github for now, to download Fitbit data. It uses the internal API that fitbit uses to populate your dashboard. Here, they give data as granular as every 15 minutes. Also, there's daily aggregate data available. Basically, the package logs into fibit.com using your email / password, and is returned a cookie. Using that cookie, the internal API is accessible. For the time being, I can only get that cookie if you use email / password to login. Facebook and Google login are available, but I haven't put any effort into figuring out how to get a cookie if one of those methods are used to login. One could copy and paste their cookie information into R and use this package that way…

Here's a quick example of downloading and graphing step data for a given day. On this particular day, I played basketball from about 6am until 9am.

install.packages("fitbitScraper")  
library("fitbitScraper")
# just reading from file to hide pw and to make .Rmd document to work...
mypassword <- readLines("pw.txt")
cookie <- login(email="corynissen@gmail.com", password=mypassword) 
df <- get_intraday_data(cookie, what="steps", date="2015-01-10")  
library("ggplot2")  
ggplot(df) + geom_bar(aes(x=time, y=steps, fill=steps), stat="identity") + 
             xlab("") +ylab("steps") + 
             theme(axis.ticks.x=element_blank(), 
                   panel.grid.major.x = element_blank(), 
                   panel.grid.minor.x = element_blank(), 
                   panel.grid.minor.y = element_blank(), 
                   panel.background=element_blank(), 
                   panel.grid.major.y=element_line(colour="gray", size=.1), 
                   legend.position="none") 

plot of chunk unnamed-chunk-1

Here's an example of the daily data.

df <- get_daily_data(cookie, what="steps", start_date="2015-01-13", end_date="2015-01-20")  
library("ggplot2")  
ggplot(df) + geom_bar(aes(x=time, y=steps), stat="identity") + 
             xlab("") +ylab("steps") + 
             theme(axis.ticks.x=element_blank(), 
                   panel.grid.major.x = element_blank(), 
                   panel.grid.minor.x = element_blank(), 
                   panel.grid.minor.y = element_blank(), 
                   panel.background=element_blank(), 
                   panel.grid.major.y=element_line(colour="gray", size=.1), 
                   legend.position="none") 

plot of chunk unnamed-chunk-2

I have to give a should out to this guy for inspiration. I don't know perl, so I couldn't really use anything here, but it gave me confidence that I could figure it out.

I hope this is of some use to some of you R hackers out there…