Skip to content

Instantly share code, notes, and snippets.

devtools::install_github("cpsievert/pitchRx")
library(pitchRx)
# All game IDs that do not contain "mlb" are included in nonMLBgids
# In order to grab non-MLB games, you *have to* use these IDs
data(nonMLBgids)
# Grab IDs for triple A games on June 1st, 2011
# This post explains more about obtaining game IDs with regular expressions --
# http://baseballwithr.wordpress.com/2014/06/30/pitchrx-meet-openwar-4/
aaa <- nonMLBgids[grepl("2011_06_01_[a-z]{3}aaa_[a-z]{3}aaa", nonMLBgids)]
devtools::install_github("cpsievert/pitchRx")
library(pitchRx)
library(dplyr)
dat <- scrape(start = "2013-09-01", end = "2013-09-11",
suffix = c("inning/inning_all.xml", "players.xml"))
# note that gameday_link can be used as a "game identifier"
p.data <- dat$pitch %>%
select(des, px, pz, sz_bot, sz_top, pitch_type, gameday_link, num) %>%
filter(des %in% c("Called Strike", "Ball"))
library(animint)
p <- ggplot(mtcars, aes(mpg, wt)) +
geom_point(colour='grey50', size = 4) +
geom_point(aes(colour = cyl))
p2 <- p + facet_wrap(cyl~am, nrow = 3)
animint2dir(list(plot = p2), "test")
p2a <- p + facet_grid(cyl~am)
animint2dir(list(plot = p2a), "test")
library(pitchRx)
data(gids, package = "pitchRx")
# Grab gameday identifiers for every Twin's home game in 2012
min.gids <- gids[grepl("2012_[0-9]{2}_[0-9]{2}_[a-z]{6}_minmlb", gids)]
urls <- paste0(pitchRx:::gids2urls(min.gids), "/rawboxscore.xml")
library(XML2R)
obs <- XML2Obs(urls)
nms <- names(obs)
boxscore <- collapse_obs(obs[grepl("^boxscore$", nms)])
cpsievert@Carsons-MacBook-Pro:~/Desktop/github/local/animint$ R

R version 3.1.1 (2014-07-10) -- "Sock it to Me"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin13.1.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
# Load/install necessary packages
library(devtools)
#install_github("cpsievert/LDAvis")
library(LDAvis)
library(mallet)
library(Rmpfr)
library(plyr)
library(XML)
library(stringi)
@cpsievert
cpsievert / getLocations.R
Last active October 13, 2015 22:23
Function to get time-sequenced spatial locations of a baseball (optionally summarized over an arbitrary set of variables) using PITCHf/x
if (!require("dplyr")) install.packages("dplyr")
if (!require("tidyr")) install.packages("tidyr")
if (!require("pitchRx")) install.packages("pitchRx")
getLocations <- function(dat, ..., summarise = TRUE) {
# select and group by columns specified in ...
tb <- dat %>%
select(..., x0:az) %>%
group_by(...)
vars <- as.character(attr(tb, "vars"))
// Define functions to render linked interactive plots using d3.
// Another script should define e.g.
// <script>
// var plot = new animint("#plot","path/to/plot.json");
// </script>
// Constructor for animint Object.
var animint = function (to_select, json_file) {
var dirs = json_file.split("/");
dirs.pop(); //if a directory path exists, remove the JSON file from dirs
var element = d3.select(to_select);
// Define functions to render linked interactive plots using d3.
// Another script should define e.g.
// <script>
// var plot = new animint("#plot","path/to/plot.json");
// </script>
// Constructor for animint Object.
var animint = function (to_select, json_file) {
var dirs = json_file.split("/");
dirs.pop(); //if a directory path exists, remove the JSON file from dirs
var element = d3.select(to_select);
// Define functions to render linked interactive plots using d3.
// Another script should define e.g.
// <script>
// var plot = new animint("#plot","path/to/plot.json");
// </script>
// Constructor for animint Object.
var animint = function (to_select, json_file) {
var dirs = json_file.split("/");
dirs.pop(); //if a directory path exists, remove the JSON file from dirs
var element = d3.select(to_select);