In addition to obtaining social network interaction data, one can collect millions of tweets from Twitter for further text mining tasks. The method for retrieving data from Twitter is very similar to Facebook. For both social platforms, all we need is an access token to access insight data. After we have retrieved the access token, we can then use twitteR to access millions of tweets.
Getting ready
In this recipe, you need to prepare your environment with R installed and a computer that can access the Internet.
How to do it…
Perform the following steps to read data from Twitter:
First, you need to log in to Twitter and access the page of Twitter Apps at https://apps.twitter.com/. Click on Create New App:
Figure 26: Creating a new Twitter app
Fill in all required application details to create a new application:
Figure 27: Filling in the required details
Next, you can select Keys and Access Tokens and then access Application Settings:
Figure 28: Copying API key and secret
Click on the Create my access token button, and the explorer will generate an authorized access token and the secret:
Figure 29: Creating the access token and secret
Install and load the twitteR package:
> install.packages("twitteR")> library(twitteR)
Set up Twitter OAuth with the copied consumer key and consumer secret from Application Settings, and the copied access token and access secret from Your Access Token:
> consumer_key <- '<consumer_key>'> consumer_secret <- '<consumer_secret>'> access_token <- '<access_token>'> access_secret <- '<access_secret>'> setup_twitter_oauth(consumer_key,+ consumer_secret,+ access_token,+ access_secret)[1] "Using direct authentication"Use a local file to cache OAuth access credentials between R sessions?1: Yes2: NoSelection: 1Adding .httr-oauth to .gitignore
At this point, you can use the searchTwitter function to extract the top 100 search results with world cup as the search key:
> res <- searchTwitter("world cup", n=100)
How it works…
In this recipe, we use twitteR to obtain tweets from Twitter. Similar to Facebook, we need an access token before accessing any tweets. To apply an access token, one must first create an application with one login account and then fill in all the required information to create a new app.
After the app is created, we select the Keys and Access Tokens tab and find both consumer keys and secrets under a section labeled Application Settings. Scroll down further to the button named Create my access token. Clicking on this provides an access token and access token secrets under a section labeled Your Access Token.
At this point, we can now connect Twitter with twitteR. First, install and load the twitteR package. You can then copy the consumer key and consumer secret from Application Settings, and the access token and access secret from Your Access Token. This copied information can be used to set up Twitter OAuth. Finally, we can use the searchTwitter function to find the top 100 search results for the world cup keyword.