Site icon R-bloggers

Downloading your twitter feed in R

[This article was first published on Adventures in Data, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

library(twitteR)
source("~/OneDriveBusiness/Projects/Authentications/twitterOAuth.R")
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")

To leave a comment for the author, please follow the link and comment on their blog: Adventures in Data.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.