Learn R Programming

pitchRx (version 1.2)

scrape: Scrape Major League Baseball's Gameday Data

Description

Function for obtaining PITCHf/x and other related Gameday Data. scrape currently has support for files ending with: http://gd2.mlb.com/components/game/mlb/year_2011/month_04/day_04/gid_2011_04_04_minmlb_nyamlb_1/inning/inning_all.xml{inning/inning_all.xml}, http://gd2.mlb.com/components/game/mlb/year_2011/month_04/day_04/gid_2011_04_04_minmlb_nyamlb_1/inning/inning_hit.xml{inning/inning_hit.xml}, http://gd2.mlb.com/components/game/mlb/year_2011/month_04/day_04/gid_2011_04_04_minmlb_nyamlb_1/players.xml{players.xml}, or http://gd2.mlb.com/components/game/mlb/year_2011/month_04/day_04/gid_2011_04_04_minmlb_nyamlb_1/miniscoreboard.xml{miniscoreboard.xml}. It's worth noting that PITCHf/x is contained in files ending with "inning/inning_all.xml", but the other files can complement this data depending on the goal for analysis. Any collection of file names may be passed to the suffix argument, and scrape will retrieve data from a (possibly large number) of files based on either a window of dates or a set of game.ids. If collecting data in bulk, it is strongly recommended that one establishes a database connection and supplies the connection to the connect argument. See the examples section for a simple example of how to do so.

Usage

scrape(start, end, game.ids, suffix = "inning/inning_all.xml", connect, ...)

Arguments

start
character string specifying a date "yyyy-mm-dd" to commence scraping.
end
character string specifying a date "yyyy-mm-dd" to terminate scraping.
game.ids
character vector of gameday_links. If this option is used, start and end are ignored. See data(gids, package="pitchRx") for examples.
suffix
character vector with suffix of the XML files to be parsed. Currently supported options are: 'players.xml', 'miniscoreboard.xml', 'inning/inning_all.xml', 'inning/inning_hit.xml'.
connect
A database connection object. The class of the object should be "MySQLConnection" or "SQLiteConnection". If a valid connection is supplied, tables will be copied to the database, which will result in better memory management. If a connection is su
...
arguments passed onto XML2R::XML2Obs. Among other things, this can be used to switch on asynchronous downloads.

Value

  • Returns a list of data frames (or nothing if writing to a database).

See Also

If you want to add support for more file types, the XML2R package is a good place to start.

Examples

Run this code
# Collect PITCHf/x (and other data from inning_all.xml files) from
# all games played on August 1st, 2013 (using asynchronous downloads)
dat <- scrape(start = "2013-08-01", end = "2013-08-01")
#As of XML2R 0.0.5, asyncronous downloads can be performed
dat <- scrape(start = "2013-08-01", end = "2013-08-01", async = TRUE)

# Scrape PITCHf/x from Minnesota Twins 2011 season
data(gids, package="pitchRx")
twins11 <- gids[grepl("min", gids) & grepl("2011", gids)]
dat <- scrape(game.ids=twins11[1]) #scrapes 1st game only

# Create SQLite database, then collect and store data in that database
library(dplyr)
my_db <- src_sqlite("Gameday.sqlite3")
scrape(start = "2013-08-01", end = "2013-08-01", connect=my_db$con)

# Collect other data complementary to PITCHf/x and store in database
files <- c("inning/inning_hit.xml", "miniscoreboard.xml", "players.xml")
scrape(start = "2013-08-01", end = "2013-08-01", connect=my_db$con, suffix = files)

# Simple example to demonstrate database query using dplyr
# Note that 'num' and 'gameday_link' together make a key that allows us to join these tables
locations <- select(tbl(my_db, "pitch"), px, pz, des, num, gameday_link)
names <- select(tbl(my_db, "atbat"), pitcher_name, batter_name, num, gameday_link)
que <- inner_join(locations, filter(names, batter_name == "Paul Goldschmidt"),
                   by = c("num", "gameday_link"))
que$query #refine sql query if you'd like
pitchfx <- collect(que) #submit query and bring data into R

Run the code above in your browser using DataLab