Twitter + R = twitteR

The other day, while reviewing the latest update to the R-Bloggers blog, I stumbled onto something amazing. If you aren’t following the blog already, I highly recommend that you do. It is a fantastic source of information related to data analyses. It is also a fantastic location to learn about new and wonderful packages built for R.

In this particular case, I stumbled onto the package called twitteR (written by Jeff Gentry). It immediately grabbed my attention because I had no idea that a package existed that would allow R to connect with Twitter. As I dove into the documentation (see here and here for details1), ideas started flowing and I knew that I had to make myself familiar with its capabilities.

I sat down and started working through Jeff’s tutorial and the official package documentation. For those curious, I was using my trusty laptop – an almost 3-year-old MacBook Pro running OS X 10.7.4 – with R version 2.15.0. Everything was going fine until I hit section 6 – Authentication with OAuth. The idea here was to install the ROAuth package to set up R to have authorization over my personal Twitter account. That is, should I feel the need, I could post messages on my Twitter feed directly from R.

Why might I want to do this?

Well, the short answer is because it’s cool (clearly). But in my mind there is also a very practical purpose to do this. It just so happens that a function (taskStatus) is available that allows the user to send a direct message to any other user whenever R completes a certain task. That task could be as simple as calculating an average, or it could be a huge simulation. The point is, should I set up my code properly, I could be out of the office and know exactly when a simulation study has finished. I might also set up R to send a tweet should a simulation fail.

I pulled 300 tweets using twitteR at 5:00pm on July 20, 2012. The plot shows where those tweets came from.

Anyway, after doing everything that I thought I needed to do (as presented in Jeff’s tutorial) I was presented with an error. My computer was unable to make the connection to Twitter. What gives? I pondered.

After searching Google, I discovered that the problem was not with the code provided in Jeff’s excellent 8 page tutorial, but with the version of the ROAuth package I had downloaded from CRAN (a repository for R packages). CRAN currently houses version 0.9.1 which, based on comments I found on Google, has a bug. Fortunately an updated version is available online (click here to download it). After downloading and installing version 0.9.2, the code ran without a problem. I was instantly authorized to post to Twitter.

If you are interested in using twitteR, you can simply install the package in R using the install.packages(“twitteR”) command. This will allow you to pull information from Twitter, however, it will not allow you to post. To do that, you need to have the updated version 0.9.2 ROAuth package, a Twitter account, and you’ll also need to set up your system as if it were a Twitter App. None of these things are difficult, and I’ve listed the basic instructions for setting up a Mac OS X system below2:

  • If you haven’t done so already, set up a Twitter account.
  • Set up your R system as a Twitter Application by following the instructions in section 6 of Jeff’s tutorial.
  • Download ROAuth_0.9.2.tar.gz. Note where the file is saved on your system.
  • Open Terminal.
  • Change directories so that your terminal reflects the directory where you saved the ROAuth_0.9.2.tar.gz file.
  • Type R CMD Install ROAuth_0.9.2.tar.gz at the command prompt. Hit return. Once the package has been installed, exit Terminal.
  • Open R.
  • Install the twitteR package.
  • Load the ROAuth and twitteR libraries.
  • Run the following commands in R, replacing cKey and cSecret with the codes provided to you when you created your Twitter Application.
cKey<-"Your Key Here"
cSecret<-"Your Secret Here"

cred<-OAuthFactory$new(consumerKey=cKey, consumerSecret=cSecret, requestURL=requestURL, accessURL=accessURL, authURL=authURL)

  • When prompted, open a browser and enter the address that is provided.
  • Authorize R to access your Twitter account by clicking the Authorize App button.
  • Return to R and enter the 7 digit PIN provided after you clicked Authorize App.
  • Register this PIN so that you won’t have to redo these steps each and every time you open R
  • Tweet!

That’s it. You should be able to send tweets from R from your own Twitter account. I fully expect to use this functionality for all of my future simulation studies. I’m also going to explore pulling data that might be relevant to my research areas to see what interesting trends I might be able to determine using the Twitter.

Happy Tweeting!

1 Note that the latter document, written by Jeff Gentry, presents an 8 page tutorial on how to use the features of the twitteR package.

2 I’ve not tested this on a Windows system as of yet.


One thought on “Twitter + R = twitteR

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s