How to Use Your Twitter Data in Displayr

How to Use Your Twitter Data in Displayr

There is a wealth of data available for analysis from the various social media platforms including Twitter. In this post, we discuss how to load data into Displayr using the Twitter API.

To access data from Twitter, you first need to set up a developer account. This is in order to generate the access tokens and keys you’ll need. Once you have these you plug them in your API calls into Displayr using R code.

Setting up a Twitter developer account

Before you can use the Twitter API, you must first apply for a Twitter Developer account. Once set up, twitter generates the necessary access tokens. To apply, go to the Twitter Application Management page and click Apply for a developer account.  A form then asks you to provide some information describing your specific use case of the Twitter API and other related information. Complete all of the required forms and submit the application.

Once you’re application has been approved, log in to the Developer Platform, select the drop-down menu from your account name in the upper right-hand corner and select Apps. Next click the Details button for your App and then select the Keys and tokens menu.  Here you’ll find your consumer API key, consumer API secret, access token and access secret.

All of these are used to authenticate each of the API calls you make.

Setting up Authentication in Displayr

Each individual API call you make from Displayr requires authentication. To do this, we first create an R output by selecting Insert > R Output.  We will use the twitteR and ROath packages for authentication. These packages have already been installed on the Displayr R server, so we only need to load these libraries by entering the following into the R CODE section of the Object Inspector.

library("twitteR")
library("ROAuth")

Next, we store each of the access tokens and keys in a separate object.

 
consumer_key = 'your_consumer_key' 
consumer_secret = 'your_consumer_secret' 
access_token = 'your_access_token' 
access_secret = 'your_access_secret'

We then use the setup_twitter_oauth() function to authenticate using the above stored token parameters.

 
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)

Click the Calculate button and if the authentication is successful, the R output will display “Using direct authentication”.  This authentication script can now be used for each API call.

Example API calls

Now that we have built a functioning authentication script, we can execute the API calls. There is an extensive Twitter API library containing functions for retrieving user and account information, searching and curating tweets, direct messaging, engagement and media and ad tracking. A complete list of available API’s can be found on the Twitter API Reference page.

One of the most commonly used API’s is the searchTwitter API function which will execute a search of Twitter based on a supplied search string. Note that there are limits as to what can be searched, so this search may not return all possible results.  First select  Insert > R output and then enter the following to search for Tweets about the new Bohemian Rhapsody movie.

library("twitteR")
library("ROAuth")

consumer_key = 'your_consumer_key' 
consumer_secret = 'your_consumer_secret' 
access_token = 'your_access_token' 
access_secret = 'your_access_secret'

setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)

search.string = 'Bohemian Rhapsody movie'
no.of.tweets = 100
tweets = searchTwitter(search.string, n=no.of.tweets, lang="en")
search.df = twListToDF(tweets)

The code starts with the authentication script. Then we supply the search parameters: the string to search for and the desired number of tweets we want. There are several other parameters that can be passed to the search function, such as date ranges, user location, language, etc. A complete list of available arguments can be found in the twitterR cran package documentation.  These arguments are then passed to the searchTwitter function which executes the search.  The search results are returned as a list. The last line in the above example converts the list into a more readable data frame object.

The userTimeline function is another common API that is used to extract Tweets from a supplied user timeline.

library("twitteR")
library("ROAuth")

consumer_key = 'your_consumer_key' 
consumer_secret = 'your_consumer_secret' 
access_token = 'your_access_token' 
access_secret = 'your_access_secret'

setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)

tweets.user = userTimeline("displayrr", n=1000, maxID=NULL, sinceID=NULL, includeRts=TRUE)
tweets.user = twListToDF(tweets.user) #converts returned Tweets to data frame
tweets.user

This code extracts the Tweets from the Displayr Twitter timeline and converts the results to a data frame.

The available arguments which can be passed to the userTimeline function can be found in the twitterR cran package documentation.

Adding Twitter data to Displayr as an R data set

In the examples above, we created R outputs to display results of the API calls. We then converted the results into a data frame, making it easier to work with in Displayr.  You may, alternatively, want to bring the data into Displayr as a data set, i.e. add the data to the Data Sets tree. To do this, select Home > New Data Set to add a new data set (or select Insert > New Data Set).  Select the R icon to create an R data set. Enter the R code for your API call and add a name to your data set. In this example, I’ve used the same API call as above to extract the Displayr Twitter timeline and named the data set “displayr_timeline”.

 

Click OK to run the R code and add the data to the Data Sets tree.

 

You can now create tables, charts and visualizations from the data set as you would with any other data set loaded into Displayr.

Rate Limiting

Twitter imposes API rate limiting on a per-user (per access token) basis. The rate limits are regulated in 15 minute intervals. For example, most API calls will be throttled if you exceed 15 calls per each 15 minute window with your access token. The search API is rate limited to 180 calls per 15-minute window. You can refer to the Twitter Developer Rate Limiting page for more details.

To discover more about all the things you can do in R, check out our “R” guides

 

About Tim Ali

Tim has over 18 years experience in quantitative and qualitative market research, database design/SQL programming, and business intelligence solution development. He studied market research, statistics, operations research and database design and holds a Bachelor’s degree in marketing and a Master’s degree in management science from Cal State University, Fullerton.