Skip to content

Instantly share code, notes, and snippets.

View atbradley's full-sized avatar

Adam Bradley atbradley

View GitHub Profile
wordcount <- function(str) {
sapply(gregexpr("\\b\\W+\\b", str, perl=TRUE), function(x) sum(x>0) ) + 1
}
@atbradley
atbradley / array_array_unique.php
Created July 26, 2013 18:12
Run array_unique() on an array containing arrays (array_unique() expects strings).
$outp = array_map('serialize', $outp);
$outp = array_unique($outp);
$outp = array_map('unserialize', $outp);
@atbradley
atbradley / pcp.paper.js
Created September 3, 2013 13:56
Draw an interactive parallel coordinates plot using Paper.js.
//Warning: Ugly code; first project with paper.js.
var lblHeight = 0;
var marX = 30;
var marY = 10;
var marDesc = 3;
var labels = [];
var dataurl = '/data/states.json';
//var dataurl = 'data/mtcars.json';
@atbradley
atbradley / blog-to-solr.py
Created April 2, 2015 12:40
"Read" a Wordpress blog and update documents indexed in Solr with links to relevant posts, based on tags. Written to index the catalog for the Hall-Hoag collection (http://library.brown.edu/collatoz/info.php?id=62)
import json
from lxml import etree
import requests
from datetime import date
def findId(xml):
for cat in xml.findall('category'):
if cat.text.startswith('HH_'):
return cat.text
@atbradley
atbradley / mapvars.xsl
Last active August 29, 2015 14:20
Alter two SVG US maps for use in the Hall-Hoag catalog project.
<?xml version="1.0" encoding="windows-1252"?>
<xsl:stylesheet xmlns="http://www.w3.org/2000/svg"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl"
version="1.0">
<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyzàèìòùáéíóúýâêîôûãñõäëïöüÿåæœçðø'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZÀÈÌÒÙÁÉÍÓÚÝÂÊÎÔÛÃÑÕÄËÏÖÜŸÅÆŒÇÐØ'" />
@atbradley
atbradley / svg_link.js
Created May 15, 2015 17:28
Create a link (as a jQuery object) to an SVG on the current page.
@atbradley
atbradley / feedstopinboard.py
Last active August 29, 2015 14:21
Read one or more RSS feeds and add the entry links to Pinboard.
#! /usr/bin/python
import feedparser
import yaml
import pinboard
from time import mktime
import os
class RssToPinboard(object):
def __init__(self, settings):
@atbradley
atbradley / sparklines.js
Created June 15, 2015 12:27
Generate sparklines based on 'data-data' attributes of canvases.
//NOTE: Depends on paper.js.
if ('undefined' == typeof unset) function isset(x) { return !('undefined' == typeof x) }
if ( !isset(ifunset) ) function ifunset(x, def) { return ('undefined' == typeof x) ? def : x; }
//These are defaults; config variables can be set before loading this script.
var LOGGING = ifunset(LOGGING, false);
var CANVAS_SELECTOR = ifunset(CANVAS_SELECTOR, 'canvas.sparkler');
var XMARGIN = ifunset(XMARGIN, 2);
var YMARGIN = ifunset(YMARGIN, 2);
@atbradley
atbradley / linear-programming.R
Created September 7, 2015 20:45
Simple linear programming function in R, with an example.
lp <- function(givenName, givenQuantity, resources, needsChart) {
if ( !all.equal(names(resources), colnames(needsChart)[2:length(colnames(needsChart))]) ) {
warning("constraints and needsChart don't match.")
return(NA)
} else if ( dim(needsChart)[1] != 2 ) {
warning("Can only handle two products.")
return(NA)
}
sapply(givenQuantity, function(x) {
@atbradley
atbradley / choroplethr_format_labels.R
Last active November 5, 2015 12:19
Generate a state map using choroplethr and add custom-formatted labels.
c = StateChoropleth$new(df_state_demographics)
c$title = "Population by State, Percent Black"
c$set_num_colors(7)
c$show_labels = F
df_state_labels = data.frame(long = state.center$x, lat =
state.center$y, name=tolower(state.name), label = state.abb)
df_state_labels = df_state_labels[!df_state_labels$name %in% c("alaska",
"hawaii"), ]