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

install.packages(“ROAuth”)

install.packages(“twitteR”)

#Initiate/Invoke the libraries

library(“ROAuth”)

library(“twitteR”)

#necessary step for Windows to handle the SSL Part

download.file(url=”http://curl.haxx.se/ca/cacert.pem”, 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‘,

consumerSecret=’Sq5kNMbdYoxNc616urV1Ayi0rKizwePRg2tDkIUEk‘,

requestURL=’https://api.twitter.com/oauth/request_token&#8217;,

accessURL=’http://api.twitter.com/oauth/access_token&#8217;,

authURL=’http://api.twitter.com/oauth/authorize&#8217;)


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:
http://api.twitter.com/oauth/authorize?oauth_token=ZS2khFL8LZmd4XZ92yeCjcchX08E80g3uzUucv6ds
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.

registerTwitterOAuth(cred)


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:

http://yourwhatyourepeatedlydo.blogspot.in/2013/04/downloading-twitter-data-using-r.html

http://davetang.org/muse/2013/04/06/using-the-r_twitter-package/

Advertisements

14 thoughts on “Getting started with TwitteR Package

  1. Pingback: Sentiment Analysis on Twitter data using R: Part – I | My exploration in data analytics

  2. 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?

    Thanks

  3. 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].

      • 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”)
        load(“cred.RData”)
        registerTwitterOAuth(cred)

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

  4. 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=”http://curl.haxx.se/ca/cacert.pem&#8221″, destfile=”cacert.pem”) #I edited the code in post. I assume the semicolon should be a double quote.

    I always get:

    cacert.pem&#8221′
    Error in download.file(url = “http://curl.haxx.se/ca/cacert.pem&#8221”, :
    cannot open URL ‘http://curl.haxx.se/ca/cacert.pem&#8221’
    In addition: Warning message:
    In download.file(url = “http://curl.haxx.se/ca/cacert.pem&#8221”, :
    cannot open: HTTP status was ‘404 Not Found’

    Can you help me out?

    Thanks!

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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