Skip to content

Instantly share code, notes, and snippets.

@seancarmody
Last active Dec 19, 2015
Embed
What would you like to do?
Parse Google Ngram Viewer
ngram_parse <- function(html){
if (any(grepl("No valid ngrams to plot!",
html))) stop("No valid ngrams.")
cols <- lapply(strsplit(grep("addColumn", html,
value=TRUE), ","),
getElement, 2)
cols <- gsub(".*'(.*)'.*", "\\1", cols)
html <- paste(html[-(1:grep("data.addRows\\(", html))],
collapse='')
html <- gsub("\\).*", "", html)
data <- as.data.frame(t(sapply(fromJSON(html),
unlist)))
colnames(data) <- cols
return(data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment