public
Last active

  • Download Gist
NHL PBP for Tableau.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
###############################################################################
# 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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.