The twitteR library is one of the most comprehensive R bindings for an API that I have ever seen. Thanks a million to Jeff Gentry for authoring and maintaining it. It took me a while to get to know it and I thought I’d share a little trick here. I recently wanted to trace the generation of my personal data from a morning in my life (possibly more on that later…). One aspect of this is the tweets that flow into my feed. twitteR has a very useful function for this.

setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)

Now we’re authenticated and ready to go, we can call homeTimeline() with the argument retryOnRateLimit set high. Note that twitter will only allow you to retrieve so many tweets from your feed so if you want recent tweets get them while you still can.

timeLine <- homeTimeline(n = 800, retryOnRateLimit = 1000)

Now we can use some of the date functionality to subset the tweets retaining only those in the relevant time period.

beginning <-  as.POSIXct("2015-12-07 09:00:00 UTC")
end <- as.POSIXct("2015-12-07 11:00:00 UTC")

times <- lapply(timeLine, function(x) x$created)
these <- which(times > beginning & times < end)

myMorningTweets <- timeLine[these]

Now that we have the subset that we want, we can use the provided function to turn the list like object into a data frame and store it for later.

myMorningTweetsDF <- twListToDF(myMorningTweets)
write.csv(myMorningTweetsDF, "data/myMorningTweets.csv")