I am trying to write an R program using the package twitteR
, but I keep running into the rate limit. I hacked this together to try to sleep until the rate limit was finished. The count<10
was intended to keep the program moving if there were no returnable values with that string. searchedTweets
is a global variable that holds all the results. How can I make this more "R-like"? Specifically how can I correctly use the retryOnRateLimit
parameter for this? How can I better handle not finding any tweets?
searchString<-c('#BringBackOurGirls','#YesAllWomen','#HeforShe','#NotAllMen','#IceBucketChallenge','#OccupyCentral','#Ferguson','#Gamergate','#YouOKSis','#BlackLivesMatter','#StopGamerGate','#TweetLikeANeurotypical','#CancelColbert','#UmbrellaRevolution','#AmINext','#IfTheyGunnedMeDown','#WhyIStayed','#DudesGreetingDudes','#GirlsLikeUs','#MensRights','#MGTOW','#RedPill','#CheerstoSochi','#NotOneMore','#Not1More','#DonLemonReporting','#CrimingWhileWhite','#LoveIsLove','#PrayForParis','#LoveWins','#IStandWithAhmed','#IStandWithPlannedParenthood','#ShoutYourAbortion','#ProLife','#Oromoprotests','#Kony2012','#SosBlakaustralia')
searchedTweets<<-NULL
lapply(searchString,function(y){
r_tweets<-NULL
count<-0
while(length(r_tweets)<=0 && count<10){
r_tweets<-searchTwitter(y, n=50, lang="en-US", since=NULL, until=NULL,
locale=NULL, geocode=NULL, sinceID=NULL, maxID=NULL,
resultType=NULL, retryOnRateLimit=0)
Sys.sleep(60*2)#seconds
count<-count+1
cat("count: ",count," on term: ",y,"\n")
}
if(count<10){
searchedTweets<<-rbind.data.frame(searchedTweets,r_tweets)
sources<-sapply(r_tweets,function(x)x$getStatusSource())
sources<-gsub("</a>","",sources)
sources<-strsplit(sources,">")
sources<-sapply(sources,function(x)ifelse(length(x)>1,x[2],x[1]))
source_table=table(sources)
pie(source_table[source_table>10])
cat("###term: ",y," done!\n")
#pie(source_table)
}else{cat("couldnt use term: ",y)}
})