Skip to content

Instantly share code, notes, and snippets.

@cpsievert
cpsievert / server.R
Last active January 12, 2016 07:27
shiny & pitchRx: an MLB PITCHf/x visualization app for the layman.
library(shiny)
library(animation)
library(Cairo)
library(pitchRx)
library(shinyRGL)
library(rgl)
valid <- function(input, default) {
if (is.null(input)) return(FALSE)
@cpsievert
cpsievert / server.R
Created January 25, 2013 22:21
Hello Shiny Example (for demonstration)
#Taken from http://rstudio.github.com/shiny/tutorial/#hello-shiny
#All credits to Joe Cheng
library(shiny)
# Define server logic required to generate and plot a random distribution
shinyServer(function(input, output) {
# Function that generates a plot of the distribution. The function
# is wrapped in a call to reactivePlot to indicate that:
@cpsievert
cpsievert / gist:8412484
Created January 14, 2014 03:21
pbp_all.R
#template for fixing bad xml files from nba.com
library(XML)
con <- url("http://www.nba.com/games/game_component/dynamic/20130528/MIAIND/pbp_all.xml")
corrupt <- readLines(con)
close(con)
tmp <- gsub("<![CDATA[", "", corrupt, fixed=TRUE) #do I really need "&lt;![CDATA"?
file <- gsub("]]>", "", tmp, fixed=TRUE) #do I really need "]]&gt;"?
doc <- xmlParseString(xml(file))
node <- getNodeSet(doc, path="/")
l <- xmlToList(node[[1]])
@cpsievert
cpsievert / osm.R
Created January 15, 2014 19:03
Manipulating XML from Open Street Map with XML2R
setwd("~/Downloads")
obs <- XML2Obs("out.xml", url.map=TRUE)
unique(names(obs)) #you might want to re_name or add_key before collapsing
tables <- collapse(obs)
head(tables[["osm//node//tag"]])
@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']")