Skip to content

Instantly share code, notes, and snippets.

@corynissen
corynissen / gist:5389426
Created April 15, 2013 16:39
getLongURL.curl
getLongURL.curl <- function(shorturl){
# uses curl statement to get expanded url from t.co links (or any link)
# loop through until there's no location attribute... that's the long link.
newurl <- shorturl
url <- ""
while(url != newurl){
data <- system(paste0("curl -I ", newurl), intern=T)
if(sum(grepl("location: ", tolower(data))) == 0){
url <- newurl
}else{
@corynissen
corynissen / dodged vs faceted blog post
Last active December 16, 2015 08:29
dodged vs faceted blog post
# faceted
ggplot(df, aes(x=factor(game), y=score)) +
geom_bar(stat="identity",aes(fill=factor(game))) + facet_grid(.~name) +
coord_cartesian(ylim=c(50, 150)) + scale_y_continuous(breaks=a[a%%10==0]) +
scale_fill_discrete(name="Game") + xlab("") + ylab("Score")
ggsave("faceted_bowling.jpg")
# dodged
ggplot(df, aes(x=factor(name), y=score)) +
geom_bar(position="dodge", stat="identity",aes(fill=factor(game))) +
@corynissen
corynissen / 3d_model.R
Created April 25, 2013 17:41
3d_model example
library(rgl)
file <- "data.txt"
smooth <- TRUE
# this data file is a list of heights... z
# x and y are not given, I just create matrices for them
z <- read.delim(file, header=F, sep="|")
z[,1] <- NULL # first column is all nulls... junk
z <- as.matrix(z)
@corynissen
corynissen / rserver.R
Last active July 19, 2016 15:47
R socket server
server <- function(){
while(TRUE){
writeLines("Listening...")
con <- socketConnection(host="localhost", port = 6011, blocking=TRUE,
server=TRUE, open="r+")
data <- readLines(con, 1)
print(data)
response <- toupper(data)
writeLines(response, con)
close(con)
@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 / 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;
@corynissen
corynissen / google_auth_keys
Created June 13, 2013 15:31
Google auth keys and whatnot
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_auth_code_request
Created June 13, 2013 15:38
google auth code request
code_url <- paste0(auth_url, "?client_id=", client_id, "&redirect_uri=",
redirect_uri, "&response_type=code&scope=", scope)
browseURL(code_url)
@corynissen
corynissen / google_auth_token_request
Created June 13, 2013 15:40
google auth token code request
code <- "paste your code from the last step here"
token.curl.request <- paste0('curl -k --header "Content-Type: application/x-www-form-urlencoded" --data "code=',
code, '&client_id=', client_id, '&client_secret=',
client_secret, '&redirect_uri=', redirect_uri,
'&grant_type=authorization_code',
'" https://accounts.google.com/o/oauth2/token')
token <- system(token.curl.request, intern=T)
token_code <- fromJSON(paste(token, collapse=""))$access_token
@corynissen
corynissen / pred_api_example_request
Created June 13, 2013 15:41
google prediction api example request
hosted.model <- "sample.languageid"
model.url <- "https://www.googleapis.com/prediction/v1.5/hostedmodels/"
model.curl.request <- paste0('curl -k --header "Content-Type: application/json" --data \'{"input":{"csvInstance":["Como se llama?"]}}\' -H "Authorization: Bearer ', token_code, '" https://www.googleapis.com/prediction/v1.5/hostedmodels/sample.languageid/predict?key=', api_key, " --verbose")
system(model.curl.request, intern=T)