Search This Blog

Sunday, 20 July 2014

Second Step: Configuring R with Twitter.


Well i hope you are done with the installation of R console and R Studio...
Now we are interested in getting Tweets from Twitter and do some kind of analysis. But for that we need to configure R in such a way that it can communicate with Twitter. Hard??? Trust me, its not. Let's get started...

Step 1: You should have an account in Twitter. Create one if you don't have.

Step 2: Go to and log in with your Twitter credentials. The reason why we are here is that you need to create an app so that you can prove to twitter your true identification and your purpose of accessing tweets from it. Don't worry its not that terrifying. Once you have logged in please find your profile icon on the upper right corner of the screen and select "My Applications". Then select "Create a New Application. Fill in your details sensibly. It will ask you for a "website" address and in case if you don't have one, fill in with an address of any decent site. This part is just a formality. And "Callback URL" can be left blank. Please select the check box so that your application is set in a way that it can be used to sign in with Twitter.

Step 3: You will come across a  screen showing you some data. Please don't rush in closing it. It's the reason why all this hard work is done. Save the data as you will need it. Importantly "Consumer Key"  and the "Consumer Secret" is needed. This will help R to communicate with Twitter so that it can fetch data. Save "Request Token URL" and "Authorize URL".. 

Step 4: Open R studio.

Step 5: Write the following commands one by one.
            This are some packages which we need for our work. Make sure you have selected
            them from the packages window on the bottom-right side after they are    

Step 6: Now if you are using windows then you need SSL tokens. This is needed to 
             maintain a secure communication. You need to download a file. Below is the

Step 7: It's time we will use the consumer secret and key, and others.

credential <- OAuthFactory$new(consumerKey="lettersAndNumbers",

         I m extremely sorry for the alignment of the above code. All I can do is explain it
         to you. First of all, credential will store the result of statement written on right  
         side. Note this is a single command. In place of "lettersAndNumbers" substitute 
         your Key and Secret saved earlier. Press enter to run it. 

Step 8: Write credential$handshake(cainfo="cacert.pem") for Windows and if you 
             are running it on Linux you might erase the code inside parentheses.

Step 9:  You will get a response back that looks like this:

             When complete, record the PIN given to you and provide it here:
             To enable the connection, please direct your web browser to:
             (Please copy the link you get and paste it into your browser address bar)

            Please provide the pin in order to complete the handshake process. You are 
            almost done if you don't get any errors. You might be wondering that you need to
            repeat this steps again. So the answer is no. The credential object, and all of the 
            other active data, will be stored in the default workspace when you exit R or  
            R-Studio. Make sure you know which workspace it was saved in so you can get it  
            back later.

Step 10: Yes the hard work is over. Let's see if everything went right or not. Write:
                registerTwitterOAuth(credential) and if it gives TRUE then buddy you 
                are ready to go. Hard work payed you. But if errors peek in, you need to
                revisit the commands and find bugs. 

Well this is the end to my post. In my next post we are going to fetch the data from Twitter...

No comments:

Post a Comment