Skip to content

Instantly share code, notes, and snippets.

@flodel
flodel / gist:2523265
Created April 29, 2012 01:50
[R] Scrape crantastic.org for most used packages
library(plyr)
library(XML)
# build a vector of URL pages we'll want to use
urls <- paste("http://crantastic.org/popcon?page=", 1:10, sep = "")
# scrape all the data from the URLs into one big data.frame
packages.df <- ldply(urls, function(url)readHTMLTable(url)[[1]])
# turn the "Users" column from factor to numeric
@flodel
flodel / gist:2523299
Created April 29, 2012 01:54
[R] Create word cloud of most used packages
library(wordcloud)
wordcloud(packages.df$`Package Name`,
packages.df$Users,
max.words = 50,
colors = brewer.pal(9, "Greens")[4:9])
@flodel
flodel / image.gallery.R
Created May 2, 2012 04:04
Basic Craigslist API
@flodel
flodel / optical.art.R
Created July 17, 2012 04:47
[R] optical art
optical.art <- function(P, N, colors = brewer.pal(P, "Greys")) {
## This function creates an Op art figure as can be found on
## http://r-de-jeu.blogspot.com/
##
## Inputs:
## - P: (integer) number of starting points
## - N: (integer) number of semi-lines starting from each point.
## For best results, N should be a multiple of P
## - colors: vector of colors for filling polygons.
@flodel
flodel / decode.R
Last active February 23, 2017 01:29
decode <- function(x, search, replace, default = NULL) {
# build a nested ifelse function by recursion
decode.fun <- function(search, replace, default = NULL)
if (length(search) == 0L) {
function(x) if (is.null(default)) x else rep(default, length(x))
} else {
function(x) ifelse(x == search[1L], replace[1L],
decode.fun(tail(search, -1L),
tail(replace, -1L),
basket <- c("apple", "banana", "lemon", "orange",
"orange", "pear", "cherry")
ifelse(basket == "banana", "apple",
ifelse(basket == "orange", "pineapple",
basket)) # or "fig"))
# [1] "apple" "apple" "lemon" "pineapple"
# [4] "pineapple" "pear" "cherry"
decode(basket, search = c("banana", "orange"),
replace = c("apple", "pineapple"))
# [1] "apple" "apple" "lemon" "pineapple" "pineapple" "pear" "cherry"
decode(basket, search = c("banana", "orange"),
replace = c("apple", "pineapple"),
default = "fig")
# [1] "fig" "apple" "fig" "pineapple" "pineapple" "fig" "fig"
@flodel
flodel / ok.comma.R
Last active January 19, 2019 12:36
ok.comma <- function(FUN) {
function(...) {
arg.list <- as.list(sys.call())[-1L]
len <- length(arg.list)
if (len > 1L) {
last <- arg.list[[len]]
if (missing(last)) {
arg.list <- arg.list[-len]
}
}
@flodel
flodel / peachtree_2015
Created July 12, 2015 13:11
Script to scrape the 2015 AJC Peachtree Road Race results
#############################################################################
## DOWNLOAD and PARSE data
base_url <- "http://trackshackresults.com/peachtree/results/2015/ptresults.php"
divisions_map <- read.table(text = '
Ind Div Division
1 1B "****MEN -- OPEN****"
2 1G "****WOMEN -- OPEN****"