Last active
December 19, 2015 19:48
-
-
Save seancarmody/6008240 to your computer and use it in GitHub Desktop.
Parse Google Ngram Viewer
This file contains 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
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