Skip to content

Instantly share code, notes, and snippets.

@corynissen
corynissen / rclient.R
Last active April 16, 2020 14:23
R socket client example
client <- function(){
while(TRUE){
con <- socketConnection(host="localhost", port = 6011, blocking=TRUE,
server=FALSE, open="r+")
f <- file("stdin")
open(f)
print("Enter text to be upper-cased, q to quit")
sendme <- readLines(f, n=1)
if(tolower(sendme)=="q"){
break
@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 / 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 / getList.R
Last active September 28, 2016 03:15
Get list of bus stop files
library(XML)
library(httr)
url1 <- "http://www.district87.org/pages/Bloomington_School_District_87/Parents_and_Students/Bus_Routes/Bloomington_High_School"
x <- GET(url1)
text <- content(x, as="text")
doc <- htmlParse(text)
a <- xpathSApply(doc, "//table[@id='fsvItemsTable']/tr")
links <- xpathSApply(a[[1]], "//a/@href")
links <- links[grepl("^/files", links)]
@corynissen
corynissen / yahoo_ff_graph.R
Created December 6, 2013 15:48
create yahoo fantasy football team outcome graph
# create a plot of my game outcomes.
p1 <- ggplot(my.df, aes(x=game, y=score, color=team, group=team)) +
geom_point() + geom_line() + scale_y_continuous()
ggsave("FF_regular_season.jpg")
@corynissen
corynissen / get_df.R
Created December 6, 2013 15:46
create df from standings vectors
my.df <- data.frame(cbind(game=rep(1:length(my.score), 2),
team=c(rep("me", length(my.score)), rep("them", length(my.score))),
score=as.numeric(c(my.score, opp.score))))
my.df$game <- factor(my.df$game, levels=1:13)
my.df$score <- as.numeric(as.character(my.df$score))
@corynissen
corynissen / team_standings_yahoo_FF.R
Created December 6, 2013 15:45
get team standings info from yahoo fantasy football API
my.team.matchups.json <- GET(paste0(team.url, my.team.key,
"/matchups?format=json"), sig)
my.team.matchups.list <- fromJSON(as.character(my.team.matchups.json), asText=T)
# get the opponent scores for my matchups for the entire season
tmp <- my.team.matchups.list$fantasy_content["team"][[1]][[2]]$matchups
opp.score <- tmp$'0'$matchup$`0`$teams$`1`$team[[2]]$team_points["total"]
opp.score <- c(opp.score, sapply(as.character(1:12),
function(x)tmp[x][[x]]$matchup$`0`$teams$`1`$team[[2]]$team_points$total))
my.score <- tmp$'0'$matchup$`0`$teams$`0`$team[[2]]$team_points["total"]
@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)
@corynissen
corynissen / get_yahoo_keys.R
Created December 6, 2013 15:39
get game key, league key and team keys
# need to get game id for my league...
ff.url <- "http://fantasysports.yahooapis.com/fantasy/v2/game/nfl?format=json"
game.key.json <- GET(ff.url, sig)
game.key.list <- fromJSON(as.character(game.key.json), asText=T)
game.key <- game.key.list$fantasy_content$game[[1]]["game_key"]
# my personal leagueid, you will have to use your own, mine is private
league.id <- "418826"
league.key <- paste0(game.key, ".l.", league.id)
league.url <- "http://fantasysports.yahooapis.com/fantasy/v2/league/"
@corynissen
corynissen / pythonclient.py
Last active July 19, 2016 15:48
python socket client example
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 6011))
while 1:
data = raw_input ( "Enter text to be upper-cased, q to quit\n" )
client_socket.send(data)
if ( data == 'q' or data == 'Q'):
client_socket.close()
break;