-
-
Save jebyrnes/1474802 to your computer and use it in GitHub Desktop.
Code for searching Twitter using the twitteR #rstats package.
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
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