Last active
August 29, 2015 13:57
-
-
Save blmoore/9400832 to your computer and use it in GitHub Desktop.
For blog post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("jsonlite") | |
library("stringr") | |
oauth <- scan("yourOAuth.token", "character") | |
getCMD <- function(user){ | |
# crude system command for querying Github API | |
newcmd <- paste0("curl -H 'Accept: application/vnd.github.v3.text-match+json Authorization: token ", | |
oauth, "' 'https://api.github.com/search/code?q=set.seed+in:file+language:R+user:", | |
user,"&page=1&per_page=500' | grep 'fragment' -") | |
return(newcmd) | |
} | |
getPRNGseeds <- function(user){ | |
print(user) | |
api.result <- system(getCMD(user), intern=T) | |
if(is.null(attr(api.result, "status"))) { | |
seeds <- cbind(user, str_match(api.result, | |
"set\\.seed\\((\\d+)\\)")[,2]) | |
Sys.sleep(10) | |
return(seeds) | |
} else { | |
cat(user, " failed") | |
return(cbind(user, NA)) | |
} | |
} | |
# Get list of prolific R devs | |
names <- unique(read.csv("bigQuery_results.csv", | |
stringsAsFactors=F)[,1]) | |
getPRNGseeds <- Vectorize(getPRNGseeds) | |
seedlist <- getPRNGseeds(names) | |
seed <- as.data.frame(do.call(rbind, seedlist)) | |
seed <- seed[!is.na(seed$V2),] | |
getPalette <- colorRampPalette(brewer.pal(9, "Set1")) | |
nrow(seed) | |
pdf("PRNG.pdf", 6, 10) | |
ggplot(seed, aes(x=reorder(V2, V2, function(x)-length(x)), | |
y=..count.., fill=user, drop=T)) + | |
geom_bar(stat="bin") + scale_y_continuous(expand=c(0,0)) + | |
scale_fill_manual(values=getPalette(15), | |
guide=guide_legend(ncol=3, nrow=5)) + | |
coord_flip() + theme_classic() + | |
theme(legend.position=c(.7,.8)) + | |
labs(list(x="PRNG seed", y="Number of appearances", | |
fill="Github username")) | |
dev.off() | |
library("wordcloud") | |
set.seed(42) | |
words <- table(seed$V2) | |
words <- data.frame(words=names(words), freq=words) | |
wordcloud(words$freq.Var1, words$freq.Freq, min.freq=1, | |
colors=brewer.pal(9, "Paired"), | |
random.color=T, scale=c(6,1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment