Skip to content

Instantly share code, notes, and snippets.

View hrbrmstr's full-sized avatar
💤
#tired

boB Rudis hrbrmstr

💤
#tired
View GitHub Profile
@hrbrmstr
hrbrmstr / demotivate-scrape.R
Created September 22, 2014 21:46
a demotivating httr example - scrape quotes from http://www.despair.com/demotivators.html
library(rvest)
library(httr)
dem <- html_session("http://www.despair.com/demotivators.html",
user_agent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.78.2 (KHTML, like Gecko) Version/7.0.6 Safari/537.78.2"))
quotes <- data.frame(category=dem %>% html_nodes(xpath="//div/a/h3") %>% html_text(),
text=dem %>% html_nodes(xpath="//div[@class='tilecontents']/p") %>% html_text(),
image_url=dem %>% html_nodes(xpath="//img[@class='tileimg']") %>% html_attr("src"))[-1,]
library(httr)
srss <- GET("http://aa.usno.navy.mil/cgi-bin/aa_rstablew.pl",
query=list(FFX="2", xxy="2014", type="0", place="Southern Maine",
xx0="-1", xx1="43", xx2="",
yy0="1", yy1="70", yy2="",
zz1="5", zz0="-1"), verbose())
readings <- strsplit(content(srss, as="text"), "\n")
library(maptools)
library(ggplot2)
library(gridExtra)
library(scales)
# adapted from http://r.789695.n4.nabble.com/maptools-sunrise-sunset-function-td874148.html
ephemeris <- function(lat, lon, date, span=1, tz="UTC") {
lon.lat <- matrix(c(lon, lat), nrow=1)
@hrbrmstr
hrbrmstr / areachoropleth.R
Last active November 1, 2016 08:48
Area Choropleth - R version of http://bl.ocks.org/mbostock/4206573
library(rgeos)
library(rgdal) # needs gdal > 1.11.0
library(ggplot2)
# map theme
devtools::source_gist("https://gist.github.com/hrbrmstr/33baa3a79c5cfef0f6df")
# topojson from the bl.ocks example
map = readOGR("us.json", "counties")
@hrbrmstr
hrbrmstr / blockycounties.R
Last active January 10, 2016 22:11
Block Counties - R version of http://bl.ocks.org/mbostock/4206857
library(rgeos)
library(rgdal) # needs gdal > 1.11.0
library(ggplot2)
# map theme
devtools::source_gist("https://gist.github.com/hrbrmstr/33baa3a79c5cfef0f6df")
# use the topojson from the bl.ocks example
map = readOGR("us.json", "counties")
@hrbrmstr
hrbrmstr / countycircles.R
Created September 26, 2014 03:39
County Circles (OK, Ovals) - R version of http://bl.ocks.org/mbostock/4206975
library(rgeos)
library(rgdal) # needs gdal > 1.11.0
library(ggplot2)
# map theme
devtools::source_gist("https://gist.github.com/hrbrmstr/33baa3a79c5cfef0f6df")
# adapted from http://stackoverflow.com/questions/6862742/draw-a-circle-with-ggplot2
# computes a circle from a given diameter. we add "id" so we can have one big
# data frame and group them for plotting
@hrbrmstr
hrbrmstr / animatedhurricanetracks.R
Created October 8, 2014 03:16
Animated Hurricane Tracks
library(maps)
library(data.table)
library(dplyr)
library(ggplot2)
library(grid)
library(RColorBrewer)
# takes in a numeric vector and returns a sequence from low to high
rangeseq <- function(x, by=1) {
rng <- range(x)
library(data.table)
library(dplyr)
library(tidyr)
library(ggplot2)
library(scales)
library(grid)
library(statebins)
library(gridExtra)
# http://www.washingtonpost.com/wp-srv/special/investigative/asset-seizures/data/all.json
@hrbrmstr
hrbrmstr / dns.cpp
Created October 26, 2014 01:47
DNS lookups in R via sourceCpp and using only standard libraries (non-vectorized)
#include <Rcpp.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace Rcpp ;
@hrbrmstr
hrbrmstr / gallup.R
Last active August 29, 2015 14:08
gallup crime survey summary plots
library(reshape2)
library(ggplot2)
library(scales)
library(RColorBrewer)
dat <- read.table(text="Crime Frequently Occassionally Rarely_Never Does_not_apply rank
Credit_card_hacked 41 28 29 2 1
Computer_smartphone_hacked 34 28 35 4 2
Home_burglarized_not_present 18 27 55 1 3
Car_stolen_not_present 15 27 56 2 4