Extracting Tweets

What you need

  1. ROAuth package
  2. twitteR package
  3. App in twitter to get consumer key and consumer secret.

To Create an App in Twitter

goto https://dev.twitter.com/

Goto Manage Apps in the bottom

Create App

To Access Tweets

To access tweets from R, you will need the consumer key and consumer secret of your app,which you can access from ‘Manage Apps’ in dev.twitter.com

Once that is done download “cacert.pm” file

download.file (url="http://curl.haxx.se/ca/cacert.pem",destfile="cacert.pem")

Create Twitter Authentication Credential Object

cred <- OAuthFactory$new (consumerKey, consumerSecret, requestURL='https://api.twitter.com/oauth/request_token', accessURL='https://api.twitter.com/oauth/access_token', authURL='https://api.twitter.com/oauth/authorize')

cred$handshake (cainfo="cacert.pem") #handshake

This will prompt you for a PIN,which you will get from the link displayed on screen.

Save the credential file for later use/sessions

save (cred, file="twitter authentication.Rdata")
load ("twitter authentication.Rdata") #to load in later session

Registering - should output TRUE if all is well

registerTwitterOAuth (cred)

Extracting Tweets

To get tweets from a particular user

userTimeline ('UserName',n=number of tweets,cainfo="cacert.pem")

To extract all tweets with a particular hashtag or user mention tweets <- searchTwitter (search.string, n=no.of.tweets, cainfo="cacert.pem", lang="en")

Summary
Reviewer
Patrick
Review Date
Author Rating
5
  • rcodeprogramming

    How to create an app in twitter? Need to explain with a few thumbnails shown ..