Skip to content

Instantly share code, notes, and snippets.

@corynissen
corynissen / google_prediction_api_example
Created June 13, 2013 15:46
google prediction api example
library(rjson)
library(RCurl)
client_id <- "client id here"
client_secret <- "client secret here"
redirect_uri <- "https://localhost/oauth2callback"
auth_url <- "https://accounts.google.com/o/oauth2/auth"
scope <- "https://www.googleapis.com/auth/prediction"
api_key <- "api key here"
@corynissen
corynissen / google_pred_api_output
Created June 13, 2013 15:49
google prediction api example output
{
"kind": "prediction#output",
"id": "sample.languageid",
"selfLink": "https:\/\/www.googleapis.com\/prediction\/v1.5\/hostedmodels\/sample.languageid\/predict",
"outputLabel": "Spanish",
"outputMulti": [
{
"label": "English",
"score": 0.051492
},
@corynissen
corynissen / gist:5823114
Created June 20, 2013 14:18
geom_hex question
# 4ijn-s7e5 is food inspections in Chicago data set 2010 to present
# may take a minute or two...
df <- read.csv("http://data.cityofchicago.org/views/4ijn-s7e5/rows.csv",
stringsAsFactors = F)
names(df) <- tolower(names(df))
# we don't want missing values in lat or lon
df <- subset(df, !is.na(longitude) & !is.na(latitude))
# let's look at complaints / food poisonings...
@corynissen
corynissen / gist:6510342
Created September 10, 2013 14:37
httr google prediction api example
library(httr)
google <- oauth_endpoint(NULL, "auth", "token", base_url = auth_url)
myapp <- oauth_app("google", client_id, secret=client_secret)
# this will open a browser window and ask permission for access
cred <- oauth2.0_token(google, myapp, scope = scope)
@corynissen
corynissen / Geocode foodborne addresses
Created November 25, 2013 15:28
Code to do the geocoding of the foodborne addresses.
library(RCurl)
library(RJSONIO)
library(ggmap)
df <- read.csv("submissions-2013-11-05.csv", stringsAsFactors=F)
names(df) <- tolower(names(df))
# order by id, or basically date
df <- df[order(df$id),]
df$lat <- df$lng <- rep(NA, nrow(df))
@corynissen
corynissen / OSM foodborne map
Created November 25, 2013 15:45
Create a map from OpenStreetMaps package in R
library(ggplot2)
library(OpenStreetMap)
merc <- projectMercator(df2$lat, df2$lng) #OSM uses mercator
# give openmap the upper right and lower left corners you want for your map
mp <- openmap(c(42.05, -87.96), c(41.62, -87.5), type="osm", minNumTiles=16)
p <- autoplot(mp) + geom_point(aes(x=merc[,1], y=merc[,2]), alpha=.7, size=10) +
theme(line = element_blank(),
text = element_blank(),
line = element_blank(),
@corynissen
corynissen / ggmap foodborne map
Created November 25, 2013 15:49
ggmap in R for foodborne
p2 <- qmap("chicago", darken=.1) + geom_point(data=df2, aes(x=lng, y=lat)) +
coord_cartesian(xlim=c(-87.96, -87.5), ylim=c(41.62, 42.05))
ggsave(plot=p2, "foodborne_p2.png", height=5, width=5)
@corynissen
corynissen / ggmap shapefile overlay
Created November 25, 2013 15:52
overlay shapefile on a ggmap
shapefile <- readOGR("chicago_city_shapefiles", "City_Boundary")
shapefile.converted <- spTransform(shapefile, CRS("+proj=longlat +datum=WGS84"))
p3 <- p2 + geom_polygon(aes(x = long, y = lat, group=group), alpha=.2, fill="black",
data = shapefile.converted) +
coord_cartesian(xlim=c(-87.96, -87.5), ylim=c(41.62, 42.05))
ggsave(plot=p3, "foodborne_p3.png", height=5, width=5)
@corynissen
corynissen / oauth1-yahoo.r
Created November 27, 2013 14:42
Example of an httr oauth for the yahoo APIs
library(httr)
# 1. Find OAuth settings for yahoo:
# Endpoints described here...
# http://developer.yahoo.com/oauth/guide/oauth-auth-flow.html
yahoo <- oauth_endpoint("get_request_token", "request_auth", "get_token",
base_url = "https://api.login.yahoo.com/oauth/v2/")
# 2. Register an application at...
# https://developer.apps.yahoo.com/dashboard/createKey.html
@corynissen
corynissen / yahoo_ff_auth.R
Created December 6, 2013 15:35
yahoo fantasy football API auth
library(httr)
# saved my yahoo keys to a file, now, read them in...
creds <- read.table("~/cn/personal/keys/yahoo.txt", stringsAsFactors=F)
consumer.key <- creds[1,1]
consumer.secret <- creds[2,1]
token.url <- "https://api.login.yahoo.com/oauth/v2/"
yahoo <- oauth_endpoint("get_request_token", "request_auth", "get_token",
base_url = token.url)