Showing posts with label CRAN. Show all posts
Showing posts with label CRAN. 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, November 6, 2014

geocodeHERE 0.1 is on CRAN

In my previous blog post, I detailed how I created my first R package called geocodeHERE. This package is a convenient wrapper for Nokia's HERE geocoding API. The cool thing about this API is that it allows for bulk geocoding. So, instead of doing n API calls to geocode n addresses, you can do it with just a couple API calls. Also, you can run 10,000 API calls per day vs. Google's 2,500.

Any how, I went through the process to submit the package to CRAN and it was accepted. It took me 3 attempts, but I did it and it should be replicated across the mirrors by now. You can check it out on CRAN here.

Here's how it works, starting with downloading and installing the package (you'll need the httr package installed)…

install.packages("geocodeHERE", repo="http://cran.rstudio.com",
                 dependencies=TRUE)  
## Installing package into ‘/home/c/R/x86_64-pc-linux-gnu-library/3.1’
## (as ‘lib’ is unspecified)
## trying URL 'http://cran.rstudio.com/src/contrib/geocodeHERE_0.1.tar.gz'
## ...
## * DONE (geocodeHERE)

Now, we can try to run some simple queries. Given an address or place name, it will return the latitude, longitude, or NA if it can't find anything

library(geocodeHERE)  
geocodeHERE_simple("wrigley field chicago IL")
## $Latitude
## [1] 41.95
## 
## $Longitude
## [1] -87.65
geocodeHERE_simple("navy pier chicago IL")
## $Latitude
## [1] 41.94
## 
## $Longitude
## [1] -87.7
geocodeHERE_simple("the bean chicago IL")
## [1] NA

That's kinda boring though. There exists functions in other packages to do this same thing, namely ggmap::geocode(). What's interesting about Nokia HERE's API is that you can do gecoding of many addresses in bulk.

data(chicago_landmarks)
addresses <- chicago_landmarks[,"Address"]
# tack on "chicago IL" to the end of these addresses
addresses <- paste(addresses, "chicago IL")
# make a dataframe with an id column so you can match the lat, lngs back
addresses_df <- data.frame(id=1:length(addresses), addresses=addresses)
str(addresses_df)
## 'data.frame':    375 obs. of  2 variables:
##  $ id       : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ addresses: Factor w/ 374 levels "1001 W. Belmont Avenue chicago IL",..: 127 149 160 161 345 290 312 206 318 203 ...
address_str <- df_to_string(addresses_df)
request_id <- geocodeHERE_batch_upload(address_string = address_str,
                                       email_address = "test@test.com")
# wait about 15 seconds... status should go from "running" to "completed"
Sys.sleep(15)
geocodeHERE_batch_status(request_id)
## [1] "completed"
# download the data
geocode_data <- geocodeHERE_batch_get_data(request_id)
# match it back to your original addresses dataframe
addresses_df <- merge(addresses_df, geocode_data, by.x="id", by.y="recId", all.x=T)
str(addresses_df)
## 'data.frame':    375 obs. of  16 variables:
##  $ id              : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ addresses       : Factor w/ 374 levels "1001 W. Belmont Avenue chicago IL",..: 127 149 160 161 345 290 312 206 318 203 ...
##  $ SeqNumber       : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ seqLength       : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ displayLatitude : num  41.9 41.9 41.9 41.9 41.8 ...
##  $ displayLongitude: num  -87.6 -87.6 -87.6 -87.6 -87.6 ...
##  $ houseNumber     : int  300 333 35 3600 NA 6901 860 4605 9326 4550 ...
##  $ street          : chr  "W Adams St" "N Michigan Ave" "E Wacker Dr" "N Halsted St" ...
##  $ district        : chr  "Loop" "Loop" "Loop" "Lake View" ...
##  $ city            : chr  "Chicago" "Chicago" "Chicago" "Chicago" ...
##  $ postalCode      : int  60606 60601 60601 60613 60637 60649 60611 60640 60643 60640 ...
##  $ county          : chr  "Cook" "Cook" "Cook" "Cook" ...
##  $ state           : chr  "IL" "IL" "IL" "IL" ...
##  $ country         : chr  "USA" "USA" "USA" "USA" ...
##  $ matchLevel      : chr  "houseNumber" "houseNumber" "houseNumber" "houseNumber" ...
##  $ relevance       : num  1 1 1 1 0.63 1 0.99 1 1 1 ...

All of the previous commands were done under the DEMO license keys. If you are going to use this a lot, it's probably best to get your own API keys. You can do that here.

If you have any questions, hit me up on twitter or shoot me an email at corynissen_AT_gmail.com