Skip to content

Instantly share code, notes, and snippets.

View atbradley's full-sized avatar

Adam Bradley atbradley

View GitHub Profile
@atbradley
atbradley / gist:3184120
Last active October 7, 2015 15:08
Post to Tumblr (as draft) when generating HTML from R Markdown (R Studio) No longer works--Tumblr's eliminated v1 of their API for everything but retrieving posts.
#in .Rprofile:
tumblr.user <- "myemail@example.com"
tumblr.pass <- "tumblrPassword"
library(markdown)
library(httr)
options(rstudio.markdownToHTML =
function(i,o) {
markdownToHTML(i,o,fragment.only=TRUE)
@atbradley
atbradley / gist:3370335
Created August 16, 2012 14:04
Grape-decorated import statements needed to enable Ant scp and sshexec tasks.
@GrabConfig(systemClassLoader=true)
@Grab(group='com.jcraft', module='jsch', version='0.1.48')
import com.jcraft.jsch.*;
@Grab(group='org.apache.ant', module='ant-jsch', version='1.8.4')
import org.apache.tools.ant.taskdefs.optional.ssh.Scp
@atbradley
atbradley / gistify.R
Last active October 8, 2015 18:28
Upload a file as a GitHub gist from R
#options('gh_token') should be a GitHub API token with the 'gist' scope.
#(See http://developer.github.com/v3/gists/)
library(httr)
library(rjson)
gistify <- function(filename, desc=F, public=F) {
data <- list(public = public)
if ( desc != F ) data$description = desc
@atbradley
atbradley / 015-imageColor.R
Last active December 11, 2015 23:28
R functions for reading images and analyzing colors in images. Demo at http://www.nottamuntown.com/2013/02/reading-images-in-r.html .
library(reshape)
library(ggplot2)
library(ReadImages)
ccodes <- read.csv('http://sandbox.adamtbradley.com/colors/')
#From http://is-r.tumblr.com/post/36586614678
#Read an image from the web and shape it into a data.frame.
#codeColors() uses this--I don't call it directly.
@atbradley
atbradley / server.R
Created February 8, 2013 03:01
A shiny app using Social Security baby names data aggregated by Hadley Wickham (https://github.com/hadley/data-baby-names).
library(shiny)
library(ggplot2)
#This is at http://raw.github.com/hadley/data-baby-names/master/baby-names.csv
names <- read.csv('~/R/baby-names.csv')
# Define server logic required to plot various variables against mpg
shinyServer(function(input, output) {
# Return the formula text for printing as a caption
output$caption <- reactiveText(function() {
@atbradley
atbradley / 017-radioParadise.R
Created February 25, 2013 23:17
Setup to analyze playlist data from Radio Paradise, and an HTML chart function adapted from Matt Asher
songs <- read.table('http://data.adamtbradley.com/RadioParadisePlays.txt',
sep='-', skip=1, quote='', strip.white=T,
allowEscapes=T, col.names=c('time', 'artist', 'track'))
songs$artist <- sub('&ndash;', '-', songs$artist)
songs$track <- sub('&ndash;', '-', songs$track)
summ <- table(paste(songs$artist, songs$track, sep=": "))
summ.artists = table(songs$artist)
@atbradley
atbradley / unemploymentVideo.R
Created April 4, 2013 13:41
Depicts changing US unemployment rates as a video (mp4) choropleth.
#Warning: Messy code.
#(It's been repurposed a few times.)
library(plyr)
library(ggplot2)
plotData <- function(date) {
und <- c(unemp[unemp$Date==date, -1])
und <- und[order(names(und))]
und <- t(as.data.frame(und))
@atbradley
atbradley / latts.php
Last active December 16, 2015 17:09
Ensure that an associative array contains values for all required keys and only those keys. (Basically the PHP5 equivalent of Textpattern's lAtts() function. It's one of those things I'm sick of having to figure out every time I need it.)
<?php
$defaults=array('a'=>1, 'b'=>2, 'c'=>3);
$input=array('a'=> 'boo', 'c'=>4, 'd'=>5, 'e'=>6);
$output = array_intersect_key($input+$defaults, $defaults);
//Might want to provide a warning if $unused isn't empty.
$unused = array_diff_key($input, $defaults);
@atbradley
atbradley / statelify.R
Created May 2, 2013 20:56
Generates the HTML necessary to create a map using Stately (http://intridea.github.io/stately/). stately.csv contains state names and abbreviations with the correct letter for the Stately font.
#States is a data.frame with at least the following columns:
# $color: Any string identifiable as a color by CSS (It's included in a `style` attribute.
# $letter: The letter corresponding to this state in the Stately font.
#
# states$id and states$class will be used as the `id` and `class`
# attributes of the list elements for the states. All other columns
# will be inserted as data-<column-name> attributes (in case you want
# to use them with JavaScript).
statelify <- function(states) {
define('TEMPLATE_DIR', 'path/to/my/templates');
/**
* Processes a PHP template.
*
* Looks in OCRA_TEMPLATE_DIR for {$template}.tpl, and includes it,
* probably generating an HTML page.
*
* @author Adam Bradley <hisself@adambradley.net>
*