Getting started with twitteR in R

I have asked by a few people lately to help walk them through using twitter API in R, and I’ve always just directed them to the blog post I wrote last year during the US presidential debates not knowing that Twitter had changed a few things. Having my interest peaked through a potential project at work I tried using some of my old code only to confronted with errors.

First of all, you now need to have a consumer key and secret from twitter themselves. After some research, I found it really easy to get one by going to twitter and creating a new applications.  Don’t be discouraged, anyone can get one.  Here is what the page looks like:

Screen Shot 2013-06-13 at 4.12.47 PM

Enter your name, brief description, and a website (you can use your blog or a place holder), and once you agree it will give you a screen like this where you get your consumer key and secret:key

You now have to authenticate within R by inserting your consumer key and secret into this code:

 getTwitterOAuth(consumer_key, consumer_secret)

It should spit out text and uri to get and input a pin, like:

To enable the connection, please direct your web browser to:
When complete, record the PIN given to you and provide it here:

You are now ready to use the searchTwitter() function. Since I work in real estate software, Kwelia, I wanted to do sentiment analysis for apartment hunting in manhattan, so I wrote out the following:

searchTwitter('apartment hunting', geocode='40.7361,-73.9901,5mi',  n=5000, retryOnRateLimit=1)

where “apartment hunting” is what I am searching for, the geocode is a lat long with greater circle of five miles of where the tweets are sent from (union square, manhattan), n is the number of tweets i want, and retweet modifies n to the limit of tweets available if n is too high. In this case you, I got back 177 tweets.


8 thoughts on “Getting started with twitteR in R

  1. Is that all that needs to be done? I’ve had to update my RCurl options to avoid an SSL certificate problem, and then it still gives me ‘Error: Unauthorized’ when I run getTwitterOAuth(consumer_key, consumer_secret). I also have set the Application Type to “Read, Write, and Access direct message”.

  2. Do we just start out with library(twitter),library(RoAuth) and then just use the above command
    getTwitterOAuth(consumer_key, consumer_secret)

  3. I’m getting an error when I enter this function with my consumer key: getTwitterOAuth(consumer_key, consumer_secret)

    Error in initRefFields(.self, .refClassDef, as.environment(.self), list(…)) :
    object ‘GOJPLLNz5ZUavaLfeQKsEA’ not found

    Please help me.

  4. Pingback: R - Postly
  5. Hi there, this looks useful post, but some how it does not work with my key and secret, for instance, after following your instructions, I get Error: Forbidden, am I missing something in the process?

    Thank you in advance , regards.

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