Skip to content

Instantly share code, notes, and snippets.

# see also http://cpsievert.github.io/bbscrapeR/rebound/
library(bbscrapeR)
dat <- rebound("20131101", "20131107", suffix = "shotchart_all.xml")
x <- dat$`shotchart//event`$x
y <- dat$`shotchart//event`$y
# x ranges roughly from -250 to 250 (the width of the court is 50ft)
# thus, a unit of 1/10 ft seems plausible
summary(x)
@cpsievert
cpsievert / buzzer.R
Created March 13, 2015 22:21
buzzer
library(bbscrapeR)
dat <- rebound("20131101", "20131107", suffix = "shotchart_all.xml")
ev <- dat$`shotchart//event`
subset(ev, y > 500)$time
# [1] 0 0 0 1 9 3 3 5 0 4 0 8
// 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);
d3 = function() {
var π = Math.PI, ε = 1e-6, d3 = {
version: "3.0.6"
}, d3_radians = π / 180, d3_degrees = 180 / π, d3_document = document, d3_window = window;
function d3_target(d) {
return d.target;
}
function d3_source(d) {
return d.source;
}
library(curl)
d3 <- "https://github.com/mbostock/d3/archive/v3.0.6.zip"
tmp <- tempfile()
curl::curl_download(d3, tmp, mode = "wb")
unzip(tmp)
unlink(tmp)
library(gistr)
gistr::gist_create(files = "d3-3.0.6/d3.js")
cat(runif(1e6), file = "gist_test.txt")
file.info("gist_test.txt")$size/1e6
# [1] 10.00022
gist_create("gist_test.txt")
# <gist>f509ddf25de859a1ac0f
# URL: https://gist.github.com/f509ddf25de859a1ac0f
# Description:
# Public: TRUE
# Created/Edited: 2015-03-31T01:55:36Z / 2015-03-31T01:55:36Z
# Files: gist_test.txt
This file has been truncated, but you can view the full file.
0.2024908 0.4952446 0.1279413 0.3325096 0.1238041 0.4837724 0.2494122 0.2130916 0.2645174 0.7806603 0.9234049 0.302911 0.3182749 0.4338073 0.6255942 0.6652068 0.9351384 0.3547414 0.9672963 0.1155865 0.8537008 0.144053 0.6924166 0.7299929 0.08099968 0.6852041 0.09841863 0.6583971 0.9621899 0.1833408 0.7719268 0.3554565 0.6591006 0.5061397 0.7644834 0.3175091 0.9625247 0.858907 0.4496594 0.4324533 0.5351242 0.8097172 0.9665691 0.2663687 0.7788428 0.1053515 0.6298249 0.8217793 0.2232553 0.8123357 0.5658277 0.9187215 0.08000962 0.577521 0.5332047 0.6905822 0.526234 0.5513667 0.7153657 0.8078531 0.335768 0.0814817 0.3578317 0.3081016 0.2049766 0.12081 0.4286839 0.01381404 0.9361469 0.4292265 0.04128595 0.3587255 0.7476126 0.9403061 0.473959 0.5930643 0.4879804 0.7987585 0.8465157 0.2007247 0.9764681 0.7359473 0.7236403 0.3862111 0.6408829 0.7908777 0.428001 0.0004567902 0.1545169 0.5864756 0.7988812 0.2473087 0.4616302 0.4661077 0.16938 0.5375322 0.3627247 0.207117 0.8670176 0.3526958 0.3950149 0.4231128 0.0796855
@cpsievert
cpsievert / pitch_tour.R
Last active August 29, 2015 14:19
Convenience function for touring baseball PITCHf/x data
library(tourr)
library(animint)
pitch_tour <- function(dat, color_by = "pitch_type", vars = c("start_speed", "break_y", "break_angle", "break_length"),
nprojs = 200, out_dir = tempdir()) {
kept <- dat[names(dat) %in% vars]
kept[] <- lapply(kept, as.numeric)
idx <- complete.cases(kept)
kept <- kept[idx, ]
type <- dat[idx, color_by, drop = TRUE]
// 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);