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.

<span class="n">library</span><span class="p">(</span><span class="n">twitteR</span><span class="p">)</span><span class="w">
</span><span class="n">source</span><span class="p">(</span><span class="s2">"~/OneDriveBusiness/Projects/Authentications/twitterOAuth.R"</span><span class="p">)</span><span class="w">
</span><span class="n">setup_twitter_oauth</span><span class="p">(</span><span class="n">consumer_key</span><span class="p">,</span><span class="w"> </span><span class="n">consumer_secret</span><span class="p">,</span><span class="w"> </span><span class="n">access_token</span><span class="p">,</span><span class="w"> </span><span class="n">access_secret</span><span class="p">)</span><span class="w">
</span>

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.

<span class="n">timeLine</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">homeTimeline</span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">800</span><span class="p">,</span><span class="w"> </span><span class="n">retryOnRateLimit</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1000</span><span class="p">)</span><span class="w">
</span>

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

<span class="n">beginning</span><span class="w"> </span><span class="o"><-</span><span class="w">  </span><span class="n">as.POSIXct</span><span class="p">(</span><span class="s2">"2015-12-07 09:00:00 UTC"</span><span class="p">)</span><span class="w">
</span><span class="n">end</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">as.POSIXct</span><span class="p">(</span><span class="s2">"2015-12-07 11:00:00 UTC"</span><span class="p">)</span><span class="w">

</span><span class="n">times</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">lapply</span><span class="p">(</span><span class="n">timeLine</span><span class="p">,</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="n">x</span><span class="o">$</span><span class="n">created</span><span class="p">)</span><span class="w">
</span><span class="n">these</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">which</span><span class="p">(</span><span class="n">times</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">beginning</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">times</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">end</span><span class="p">)</span><span class="w">

</span><span class="n">myMorningTweets</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">timeLine</span><span class="p">[</span><span class="n">these</span><span class="p">]</span><span class="w">
</span>

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.

<span class="n">myMorningTweetsDF</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">twListToDF</span><span class="p">(</span><span class="n">myMorningTweets</span><span class="p">)</span><span class="w">
</span><span class="n">write.csv</span><span class="p">(</span><span class="n">myMorningTweetsDF</span><span class="p">,</span><span class="w"> </span><span class="s2">"data/myMorningTweets.csv"</span><span class="p">)</span><span class="w">
</span>

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.

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)