Getting started with twitteR in R

Posted: June 13, 2013 in R project posts

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.


  1. Nathan D says:

    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. Manju says:

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

  3. […] para acceder utilizando el paquete twitteR de R. Ha sido sencillo siguiendo las instrucciones en Getting started with twitteR in R. Ya que estaba en ello, le he echado un vistazo al hashtag #LeyWertNo, del que he extraído 1398 […]

  4. delanolamy says:

    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.

  5. It’s an awesoome post for aall the web viewers;
    they will get advantage from it I am sure.

  6. R - Postly says:

    […] acceder utilizando el paquete twitteR de R. Ha sido sencillo siguiendo las instrucciones en Getting started with twitteR in R. Ya que estaba en ello, le he echado un vistazo al hashtag #LeyWertNo, del que he extraído 1398 […]

  7. Lovrenc says:

    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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s