Skip to content

Instantly share code, notes, and snippets.

@cpsievert
cpsievert / animint.js
Created May 21, 2014 21:00
My animint plot
// 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);
@cpsievert
cpsievert / animint.js
Created May 21, 2014 23:02
My animint plot
// 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);
devtools::install_github("tdhock/animint", ref = "carson-test")
library(animint)
iris$id <- 1:nrow(iris)
viz <- list(petal=ggplot()+
geom_point(aes(Petal.Width, Petal.Length, fill=Species,
clickSelects=id), data=iris),
sepal=ggplot()+
geom_point(aes(Sepal.Width, Sepal.Length, fill=Species,
clickSelects=id), data=iris))
dat <- data.frame(x = 1:5, y = 1:5, p = 1:5, q = factor(1:5),
r = factor(1:5))
p <- ggplot(dat, aes(x, y, colour = p, size = q, shape = r)) + geom_point()
p + scale_colour_continuous(guide = "colorbar") + guides(colour = "none") #no guide
p + scale_colour_continuous(guide = "none") + guides(colour = guide_colorbar()) #guide
@cpsievert
cpsievert / animint.js
Last active August 29, 2015 14:02
My animint plot
// Define functions to render linked interactive plots using d3.
// Another script should define e.g.
// <script>
// var plot = new animint("#plot","plot.json");
// </script>
// Constructor for animint Object.
var animint = function (to_select, json_file) {
var element = d3.select(to_select);
this.element = element;
var Widgets = {};
@cpsievert
cpsievert / update.R
Last active August 29, 2015 14:02
Script to update a PITCHf/x database
library(dplyr)
library(pitchRx)
# This script requires pitchRx 1.5
if (packageVersion("pitchRx") < 1.5) { devtools::install_github("cpsievert/pitchRx"); library(pitchRx) }
# Be sure to specify the *entire* path to your database
db.file <- "~/pitchfx/pitchRx.sqlite3"
stopifnot(file.exists(db.file))
db <- src_sqlite(db.file)
update_db(db$con)
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)])