Downloading your twitter feed in R

December 6, 2015
By

(This article was first published on Adventures in Data, and kindly contributed to R-bloggers)

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)