Skip to content

Instantly share code, notes, and snippets.

@cpsievert
cpsievert / server.R
Created February 6, 2014 04:27
ECDF app with shiny & ggvis
library(shiny)
library(ggvis)
x <- seq(-4, 4, .1)
dat <- data.frame(x, y=dnorm(x))
g1 <- ggvis(dat, props(x = ~x, y = ~y), mark_path())
shinyServer(function(input, output, session) {
# A subset of mtcars
dat2 <- reactive({ dat[dat$x <= input$z,] })
@cpsievert
cpsievert / power.R
Created February 8, 2014 04:47
XML2R Example for PowerPoolMonitoring
#This example will require version 0.0.5 of XML2R
library(devtools)
install_github("XML2R", "cpsievert")
library(XML2R)
#you could use multiple files here if you wanted...
files <- "https://www.misoenergy.org/ria/Consolidated.aspx?format=xml"
obs <- XML2Obs(files)
names(obs)[grep("PricingNode", names(obs))] <- "PricingNode"
tables <- collapse_obs(obs)
head(tables[[5]])
@cpsievert
cpsievert / oecd.R
Created March 4, 2014 21:26
Scrape OECD Data
library(XML2R)
file <- "http://stats.oecd.org/restsdmx/sdmx.ashx/GetData/UN_DEN/AUS+CAN+FRA+DEU+NZL+GBR+USA+OECD/OECD?startTime=1960&endTime=2012"
obs <- XML2Obs(file)
# Rename observations so we can 'recycle' country labels to time/value
nms <- names(obs)
nms[grepl("SeriesKey//Value$", nms)] <- "root"
nms[grepl("Obs//Time$", nms)] <- "root//time"
nms[grepl("Obs//ObsValue$", nms)] <- "root//value"
@cpsievert
cpsievert / mercy.R
Created March 10, 2014 05:02
Script used to create figure for first analyzing baseball data with R post
library(pitchRx)
library(dplyr)
library(mgcv)
# Establish a SQLite database connection
my_db <- src_sqlite("pitchRx.sqlite3")
# DISLCAIMER: this 'pitchfx.sqlite3' database was obtained using pitchRx version 1.2
# The code below probably won't work if you are using data collected from earlier versions or other methods
# Anyway, if you want to recreate this analysis, make sure you have pitchRx 1.2 or higher, then run:
@cpsievert
cpsievert / index.Rmd
Last active August 29, 2015 13:57
First stab at knitrBootstrap
---
output:
knitrBootstrap::bootstrap_document:
title: "Stat 226 Home Page"
theme: amelia
highlight: sunburst
theme.chooser: TRUE
highlight.chooser: TRUE
bootstrap.panel: FALSE
---
@cpsievert
cpsievert / elife.R
Created March 30, 2014 21:33
Obtain all abstracts from DOIs scraped off of -- https://github.com/elifesciences/elife-articles
library(XML)
library(RCurl)
library(stringr)
library(elife)
# Obtain all the dois! Well, at least every one on GitHub.
# Should/could this be an option in searchelife? It doesn't take that long...
con <- getURL("https://github.com/elifesciences/elife-articles")
doc <- htmlParse(con, asText = TRUE)
nodes <- getNodeSet(doc, path="//a[@class='js-directory-link']")
library(pitchRx)
library(colorspace)
vals <- rainbow_hcl(361)
data(pitches)
pitches$spin_dir <- with(pitches, factor(round(spin_dir), levels = seq(0, 360)))
p1 <- strikeFX(pitches, color = "spin_dir", point.alpha = 0.3,
adjust = TRUE, contour = TRUE) + facet_grid(pitch_type ~ stand) +
theme(legend.position = "none") +
scale_colour_manual(values = vals) + coord_equal()
@cpsievert
cpsievert / animint.js
Created May 21, 2014 18:41
My first 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);
library(devtools)
install_github("ramnathv/rCharts")
# This will soon have to be install_github("rOpenSci/gistr"); see convo here https://twitter.com/ramnath_vaidya/status/469160178898989056
library(rCharts)
# set your GitHub username and password
options(github.username = "foo", github.password = "bar")
animint2gist <- function(plot.list, out.dir = tempfile(), json.file = "plot.json", open.blocks = interactive()){
gg2animint(plot.list, out.dir, json.file, open.browser = FALSE)
# use a flat file structure!
@cpsievert
cpsievert / animint.js
Created May 21, 2014 19:40
My first 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);