Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@seancarmody
Last active December 19, 2015 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seancarmody/6008240 to your computer and use it in GitHub Desktop.
Save seancarmody/6008240 to your computer and use it in GitHub Desktop.
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