Skip to content

Instantly share code, notes, and snippets.

@jonahlyn
Created December 28, 2014 00:35
Show Gist options
  • Save jonahlyn/3cf4c4428a9abe641d2d to your computer and use it in GitHub Desktop.
Save jonahlyn/3cf4c4428a9abe641d2d to your computer and use it in GitHub Desktop.

Revisions

  1. jonahlyn created this gist Dec 28, 2014.
    47 changes: 47 additions & 0 deletions gistfile1.r
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    ###########################################################################
    # Read in activity data from Jawbone UP api in R
    # 1. Setup an application at: https://jawbone.com/up/developer/
    # 2. Set the OAuth redirect URLs: http://localhost:1410, http://localhost:1410/
    # 3. Be sure to set your key and secret in R:
    # Sys.setenv(JAWBONE_API_KEY = "Your Client Id",
    # JAWBONE_CONSUMER_SECRET = "Your App Secret")
    # 4. Install httr package: install.packages("httr")
    ###########################################################################
    library("httr")

    # Read in existing data from a file or from the api if the file does not exist.
    filename <- "jawbone.txt"
    if(file.exists(filename)) {
    load(filename)
    } else {
    jawbone <- oauth_endpoint("token", "auth", "token", base_url = "https://jawbone.com/auth/oauth2")
    jawbone.api.key <- Sys.getenv(c("JAWBONE_API_KEY"))
    jawbone.app <- oauth_app("jawbone", jawbone.api.key)

    # Full list of possible scopes:
    # basic_read, extended_read, location_read, friends_read, mood_read, mood_write, move_read, move_write,
    # sleep_read, sleep_write, meal_read, meal_write, weight_read, weight_write, cardiac_read, cardiac_write,
    # generic_event_read, generic_event_write
    jawbone.token <- oauth2.0_token(jawbone, jawbone.app,
    c("basic_read", "extended_read", "location_read",
    "friends_read", "mood_read", "move_read",
    "sleep_read", "meal_read", "weight_read",
    "generic_event_read"))


    request.url <- c("https://jawbone.com/nudge/api/v.1.1/users/@me/moves")
    req <- GET(request.url, config(token = jawbone.token))
    stop_for_status(req)
    moves.data <- content(req)
    save(moves.data, file = filename)
    }

    # Create a simple data frame with date and total number of steps taken
    steps.df <- data.frame(date = as.Date(character()),
    steps = integer(),
    stringsAsFactors = FALSE)
    for(m in moves.data$data$items[1:moves.data$data$size]){
    steps.df <- rbind(steps.df, data.frame(date = as.Date(as.character(m$date), format = c("%Y%m%d")),
    steps = m$details$steps,
    stringsAsFactors = FALSE))
    }