Created
February 13, 2011 17:17
-
-
Save Btibert3/824851 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################### | |
# Date: Feb 13, 2011 | |
# Author: @BrockTibert | |
# Filename: NHL PBP and Tableau.R | |
# R version: 2.12.1 | |
# | |
# Purpose: Grab the Play by Play event data and visualize live using Tableau | |
# | |
# | |
# Copyright (c) 2011, under the Simplified BSD License. | |
# For more information on FreeBSD see: http://www.opensource.org/licenses/bsd-license.php | |
# All rights reserved. | |
############################################################################### | |
# www.tableausoftware.com/support/knowledge-base/background-image-coordinates | |
#------------------------------------------------------------------------------ | |
# Setup | |
#------------------------------------------------------------------------------ | |
## set working directory | |
DIR <- "C:\\Users\\Brock\\Documents\\My Dropbox\\Eclipse\\Projects\\R\\NHL\\Blog Posts\\PBP and Tableau Mapping" | |
setwd(DIR) | |
## open the following libraries | |
library(XML) | |
library(RCurl) | |
library(twitteR) | |
library(stringr) | |
library(plyr) | |
## function to grab the PBP and parse to list | |
grab.pbp <- function(GAMEID="2010020677") { | |
#Takes 1 arguments: GAMEID = the scraped game id for the 1011 season | |
# returns the JSON dataset | |
URL <- paste("http://live.nhl.com/GameData/20102011/", | |
GAMEID, "/PlayByPlay.json", sep="") | |
out <- getURL(URL) | |
out <- toJSON(out) | |
out <- fromJSON(URL)$data$game$plays$play | |
return(out) | |
} | |
## function to parse the data list into a dataframe | |
parse.pbp <- function(pbp.JSON) { | |
for(i in 1:length(pbp.JSON)) { | |
z <- pbp.JSON[[i]] | |
# parse the data into a data frame | |
df.temp <- as.data.frame(t(unlist(z)), stringsAsFactors=F) | |
df.temp$seqnum <- i | |
# append the data | |
df <- rbind.fill(df, df.temp) | |
} | |
return(df) | |
} | |
## Let's grab the data | |
pbp <- data.frame() | |
for (game in c("2010020842")) { | |
# execue the fuctions | |
temp <- try(grab.pbp(GAMEID=game)) | |
if (length(temp) > 0) { | |
print(paste("# of rows: ", length(temp), sep="")) | |
temp.df <- parse.pbp(temp) | |
temp.df$gameid <- game | |
print("inserting rows") | |
pbp <- rbind.fill(pbp, temp.df) | |
} | |
rm(temp, temp.df) | |
} | |
# save the data | |
write.table(pbp, "Play by Play.csv", sep=",", row.names=F) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment