Twitter is a popular social platform for expressing our emotions, activites and also for getting a massive amount of information around the web. Using R
, we will see here how to access tweets which can be analyzed later for different purposes like text analytics. Here, we will be using the package TwitteR
for extract information from Twitter. We will also require the packages ROAuth
and RCurl
for authentication and connecting to Twitter.
First of all you need to go to the Twitter Developers page and sign up for a new account, after that you should create a new application. An application is needed for the purpose of connecting to Twitter, just provide a name, description and website address and you will get an access token and an access token secret. Keep these two access codes for later use.
Open up your R console and start by loading the following libraries
rm(list=ls()) # Clear the previously used libraries
# Load the required R libraries
library(twitteR)
library(ROAuth)
library(RCurl)
Download the curl certificate and save it in the folder of your choice.
download.file(url="http://curl.haxx.se/ca/cacert.pem",destfile="cacert.pem")
We will setup the necessary parameters and prepare the call to function OAuthFactory
to get the authentication object.
# Set constant requestURL
requestURL <- "https://api.twitter.com/oauth/request_token"
# Set constant accessURL
accessURL <- "https://api.twitter.com/oauth/access_token"
# Set constant authURL
authURL <- "https://api.twitter.com/oauth/authorize"
In the consumerKey
field paste the access token you got for your twitter developer application.
consumerKey <- "xxxxxxxxxxxxxxxxxx"
In the consumerSecret
field paste the access token you got for your twitter developer application.
consumerSecret <- "xxxxxxxxxxxxxxxxxx"
Now, create the authorization object by calling function OAuthFactory
twitCred <- OAuthFactory$new(consumerKey=consumerKey,
consumerSecret=consumerSecret,
requestURL=requestURL,
accessURL=accessURL,
authURL=authURL)
# Asking for access
twitCred$handshake(cainfo="cacert.pem")
In your R console you will see the following message instructing you to direct your web browser to the specified URL. There you will get a PIN code which you will have to type in your R console.
To enable the connection, please direct your web browser to:
https://api.twitter.com/oauth/authorize?oauth_token=xxxx
When complete, record the PIN given to you and provide it here: xxxxxx
Now, verify that your new credential is working properly
registerTwitterOAuth(twitCred)
You should get the following output in the console.
[1] TRUE
Save it for future use by downloading a Cred file to the folder of your choice
save(list="twitCred", file="twitteR_credentials")
Now its time to get some tweets. To do so, We assume that you have placed the files cacert.perm
and twitteR_credentials
in the current working directory.
library (twitteR)
## Loading required package: ROAuth
## Loading required package: RCurl
## Loading required package: bitops
## Loading required package: digest
## Loading required package: rjson
load("twitteR_credentials")
registerTwitterOAuth(twitCred)
## [1] TRUE
Now, lets get for instance, tweets about the FIFA World Cup 2014.
s <- searchTwitter('#wc2014', cainfo="cacert.pem", n=5, locale = 'en', geocode = '42.375,-71.1061111,10mi')
s
## [[1]]
## [1] "ctheisinger: RT @DXL38: Klinsmann and Loew are chatting over coffee.\n#USMNT #USAGER #DFB_Team #WMBlog #WC2014 http://t.co/96M0LuLUmG"
##
## [[2]]
## [1] "aublumberg: RT @DXL38: Klinsmann and Loew are chatting over coffee.\n#USMNT #USAGER #DFB_Team #WMBlog #WC2014 http://t.co/96M0LuLUmG"
##
## [[3]]
## [1] "ArminFakouhi: Wow. So worth staying up...what an amazing game #WC2014 #PortugalvsUSA"
##
## [[4]]
## [1] "_bxsz: RT @SportingGP: FML. “@SportingGP: #WC2014 picks for 22 Jun: #bel 2-1 #rus, #kor 1-0 #alg, #usa 2-2 #por (hoping for a #usa win though!)â€"
##
## [[5]]
## [1] "SportingGP: FML. “@SportingGP: #WC2014 picks for 22 Jun: #bel 2-1 #rus, #kor 1-0 #alg, #usa 2-2 #por (hoping for a #usa win though!)â€"
Happy tweet mining and analyzing!