Skip to content

Instantly share code, notes, and snippets.

@jebyrnes
Forked from sckott/iworkfortheinternet.R
Created December 14, 2011 01:26
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 jebyrnes/1474802 to your computer and use it in GitHub Desktop.
Save jebyrnes/1474802 to your computer and use it in GitHub Desktop.
Code for searching Twitter using the twitteR #rstats package.
require(plyr); require(stringr); require(ggplot2); require(lubridate); require(twitteR)
##need to figure out why there are two datout objects and not just 1...
twitterPlot<-function(myString, n=1500, since=NULL, until=NULL){
since2<-as.character(strptime(since, '%Y-%m-%d')+2*24*60*60)
until2<-as.character(strptime(until, '%Y-%m-%d')+2*24*60*60)
datout_1 <- searchTwitter(myString, n = n, since=since, until=until)
datout_2 <- searchTwitter(myString, n = n, since=since2, until=until2)
datoutdf <- ldply(c(datout_1, datout_2), function(x) x$toDataFrame(), .progress="text")
#what is input string 135 is invalid in this locale about
#and why is it causing scrip to fail? happens with this grep...
actual <- grep(myString, datoutdf[,1], ignore.case=T)
datoutdf2 <- datoutdf[actual,]
datoutdf2$newtime <- round_date(datoutdf2[,4], "hour")
datoutdf2_ <- ddply(datoutdf2, .(newtime), summarise,
numtweets = length(newtime))
ret<-ggplot(droplevels(datoutdf2_), aes(newtime, numtweets)) +
theme_bw(base_size=18) +
geom_line(color="red") +
labs(x='\nTime', y='Number of tweets\n') +
opts(title=paste("Tweets containing", myString, sep=" "))
return(ret)
}
#make the ggplot2 object
working<-twitterPlot("I work for the internet", n = 1500, since='2011-11-11', until='2011-12-12')
#plot and save
working
ggsave("iworkfortheinternet.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment