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 / pantone_cotd.R
Created August 29, 2014 13:57
Quick R function to get the Pantone "Color of the Day" color from https://www.pantone.com/pages/colorstrology/colorstrology.aspx
library(XML)
library(httr)
library(stringr)
pantone_cotd <- function(cotd_url = "https://www.pantone.com/pages/colorstrology/colorstrology.aspx") {
# have to fake a real user agent to get the color
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"
try(pantone <- GET(cotd_url, user_agent(agent)))
@hrbrmstr
hrbrmstr / rvestEx1.R
Created September 17, 2014 16:09
rvsest example
library(XML)
library(httr)
library(rvest)
library(magrittr)
# setup connection & grab HTML the "old" way w/httr
freak_html <- htmlParse(content(GET("http://torrentfreak.com/top-10-most-pirated-movies-of-the-week-130304/"), as="text"))
# do the same the rvest way, using "html_session" since we may need connection info in some scripts
freak <- html_session("http://torrentfreak.com/top-10-most-pirated-movies-of-the-week-130304/")
@hrbrmstr
hrbrmstr / 500miles.R
Last active August 29, 2015 14:06
More accurate Proclaimers geographical humor (in R)
library(ggplot2)
library(ggmap)
vectorDestination <- function(lonlatpoint, travelvector) {
Rearth <- 6372795
Dd <- travelvector$magnitude / Rearth
Cc <- travelvector$direction
@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")
@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 / 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
---
title: "twee demo"
author: "Jenny Bryan"
date: "17 August, 2014"
output:
html_document:
toc: TRUE
keep_md: TRUE
---
doInstall <- TRUE
toInstall <- c("XML", "maps", "ggplot2", "sp")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
myURL <- "http://en.wikipedia.org/wiki/United_States_presidential_election,_2012"
allTables <- readHTMLTable(myURL)
str(allTables) # Look at the allTables object to find the specific table we want
stateTable <- allTables[[14]] # We want the 14th table in the list (maybe 13th?)