In this article, we will discuss how to get live Twitter data into R environment. This process can be useful for data visualization and analysis. However, since the release of Twitter’s Version 1.1 API and OAuth, a virtual handshake is necessary for every request, and the app must be verified. To get started, you will need four essential things,
- YOUR API KEY
- YOUR API SECRET
- YOUR ACCESS TOKEN
- YOUR ACCESS TOKEN SECRET
To get the above four access keys/secrets, Got to https://dev.twitter.com/ and log in with your Twitter Account, if you do not have a twitter account, please create one in order to access twitter data in R.
Fill out the Name of the app, Description of the app and as twitter requires a valid URL for the website, you can just type in http://www.akanther.com; you won´t need it anymore. And just leave the Callback URL blank.
Click on Create your twitter application, you´ll get redirected to a screen with all the OAuth setting of your new App. Just leave this window in the background; you´ll need it later.
Now let’s go to the R console and write the code below to install the required libraries for Twitter
# Getting the required Library list.of.packages <- c("devtools", "rjson", "bit64", "httr","devtools","twitteR") new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])] if(length(new.packages)) install.packages(new.packages) library('devtools','rjson','httr','twitteR')
Twitter Authentication with R:
# On the twitter development page Under the newly created application, find application settings under Keys and Access Tokens, # there you will be able to locate your 1) Consumer Key and 2) Consumer Secret i.e. your API key and API Secret, # copy both individually and replace it in the code below, in quotes. # If the Access Token isn't created then please create a new Access Token and you will be provided with # 3) Access Token 4) Access Token Secret # Copy the newly created access token and secret individually and replace them in the code below, in quotes. your_api_key <- "YOUR API KEY" your_api_secret <- "YOUR API SECRET" your_access_token <- "YOUR ACCESS TOKEN" your_access_token_secret <- "YOUR ACCESS TOKEN SECRET" setup_twitter_oauth(your_api_key,your_api_secret,your_access_token,your_access_token_secret)
Now you are ready to play with twitter.
Once you have completed the above steps twitter will allow you to access its server and you will be able to download the data whenever you want.
Now let’s try some commands,
#Getting twitter data on US Elections searchTwitter("USElections") #__________________________________# #Getting twitter data on Climate Change searchTwitter("ClimateChange") #__________________________________# #Getting twitter data on iPhone searchTwitter("iphone") #__________________________________# #Hunting appartments via twitter with 5000 enteries searchTwitter('apartment hunting', n=5000, retryOnRateLimit=1) #__________________________________# #Getting last 10 tweets of a specific user : @POTUS userTimeline("POTUS", n=10) #End of Tutorial
You must log in to post a comment.