Skip to content

Instantly share code, notes, and snippets.

@pnewhook
Created February 5, 2012 20:02
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 pnewhook/1747724 to your computer and use it in GitHub Desktop.
Save pnewhook/1747724 to your computer and use it in GitHub Desktop.
R Function to Chart Clients Used for Search Results
findClients <- function(searchString,queryLimit=1000, pie=TRUE,numToPlot=10){
#thanks http://stackoverflow.com/questions/5060076/convert-html-character-entity-encoding-in-r
html2txt <- function(str) {
xpathApply(htmlParse(str, asText=TRUE),
"//body//text()",
xmlValue)[[1]]
}
library(XML)
library(twitteR)
tweets <- searchTwitter(searchString,n=queryLimit)
sources <- sapply(tweets,function(x) x$getStatusSource())
sourcesDecoded <- sapply(sources,html2txt,USE.NAMES=FALSE)
sourcesXML <- sapply(sourcesDecoded,xmlParseString,USE.NAMES=FALSE)
clients <- sapply(sourcesXML,xmlValue,USE.NAMES=FALSE)
clientsdf <- as.data.frame(table(clients))
ordered <- clientsdf[order(clientsdf$Freq,decreasing=TRUE),]
orderedSubset <- ordered[1:numToPlot,]
pie(orderedSubset$Freq,labels = orderedSubset$clients,main=searchString)
return(ordered)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment