Getting started with TwitteR Package

The intention of this blog post is to give you start on using the TwitteR Package of R. Using this package you can do lot of analysis on social media “Twitter”. I have written an post on analyzing a Cricketer’s Century Tweets and also the need for analyzing tweets already in my blog.

Pre-Requisite tools & Environment:

We are going to explore this completely with Windows 7 and R.

Steps to follow:

Step 1:We need to use the TwitteR package and ROAuth package for accessing the tweets. As per the recommendation from Twitter its always safe to access the tweets via SSL. First we will see the code for the same.

#install the basic packages



#Initiate/Invoke the libraries



#necessary step for Windows to handle the SSL Part

download.file(url=””, destfile=”cacert.pem”)

Step 2: Use the OAuthFactory to setup the Credentials and start accessing data in the following way

cred <- OAuthFactory$new(consumerKey=’azbiz8LbVeA0lBUVh3c6lA‘,





After this you can notice that “handshakeComplete” is FALSE. We need to complete the handshake to get access to the TwitterAPI and its data.

Step 3: Create a handshake with twitter, for which you will get a message like the following:

To enable the connection, please direct your web browser to:
When complete, record the PIN given to you and provide it here: install.packages("ROAuth")
Error: Unauthorized

Once you naviage to the URL you will get a PIN which you should type in the R Console. Now you can see that we have enterered the PIN from the browser after authorizing the Application.

You can also realize that now the “handshakeComplete” has become TRUE.

Step 4: Verify the status of OAuth authentication using the following command and it should return TRUE.


Step 5: Now the next step is to start accessing the data using TwitterAPI. Let’s try to get started with accessing the User Information.

userInfo<-getUser(“seesiva”, cainfo=”cacert.pem”)

You need to make sure that you also pass the cainfo otherwise you will get an SSL Error.

Hope now we understand the steps required for accessing the Twitter data using the TwitteR Package. In this example we had shown the various attributes of the User Object retrieval. In the next post we will try to analyze some data.

Similar posts for your reference:


14 thoughts on “Getting started with TwitteR Package

  1. Hi,
    I cannot get the ROAuth package to work. It come up with the message ‘package “ROAuth” is not available” Do you know why this may be?


  2. Have you ever had twitter return the pin it gives you after authorization with the number one, e.g., [1] PIN??? I try inputting it again, and it does nothing. No matter what I write it returns what I typed plus [1].

      1. Strange, I responded that I found a fix but it didn’t go up. It looks like I missed some key steps.

        library (plyr)
        library (stringr)

        and to finalize the handshake

        save(cred, file=”cred.Rdata”)

        Thanks for you response and for all of the great info on this blog!

  3. Hello,

    Thanks for posting this. This is very helpful to those people who are very interested in analytics like me. However, I am having trouble in setting up the twitteR package in R. I am having an error when I run this part:

    download.file(url=”″, destfile=”cacert.pem”) #I edited the code in post. I assume the semicolon should be a double quote.

    I always get:

    Error in download.file(url = “”, :
    cannot open URL ‘’
    In addition: Warning message:
    In download.file(url = “”, :
    cannot open: HTTP status was ‘404 Not Found’

    Can you help me out?


    1. Try with this: download.file(url=””, destfile=”cacert.pem”). Hopefully this resolves your problem and good luck with your initiative. Give your feedback. Thanks.

    2. Hi,

      Forget about my question above. I have found the correct link in the reference blog that you have cites.

      However, I have another issue encountered. When I visit the link to get the pin, I am being asked for an SSL. How do I get one? (Sorry, I am no technical person. I tried googling it and it says I have to purchase one. Do we have workaround here? Thanks!)

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