Basic Functionality

Christopher Marks

2019-01-18

This vignette works through the basic Twitter collection functions offered by the twinfoR package. The ‘US Politics’ vignettes demonstrate more advanced collection and analysis functionality.

Package Import

Let’s start by importing the twinfoR namespace. Don’t forget to install the package, if you haven’t already.

# install.packages("twinfoR.zip",repos=NULL,type="binary")
library(twinfoR)

Authentication

Authenticating an App in R can be tricky, but the authentication methods supplied by this package aim to simplify the process. These functions are

The documentation for these functions has more information about their usage.

We’ll use the INFORM-Tutorial Twitter Application (i.e., authorize_IT). Alternatively, you can supply your own application credentials using the authorize_app function.

You will need a Twitter Account to continue! If you don’t have one, you can easily create one on the Twitter website. If you do not have a Twitter account and do not wish to make one, this package will be of very limited use to you.

I recommend logging onto Twitter in your Browser before running the following line of code. Either way, you must be careful not to hit immediately after running it, for example by copying and pasting the whole line including a the ‘newline’ character into an R console. A good way to prevent this problem is to manually type the following line into the console.

auth.vector <- authorize_IT()

This function attempts to browse to a web page that allows you to authorize the INFORMS-Tutorial Twitter Application. Once you authorize the application, you will receive a PIN. You must enter this PIN exactly as it appears back into the R Console. The the Authorization page does not open automatically, you can browse to it manually using the URL written by the function in the R Console.

It is important to name the result auth.vector as shown above, as the functions in the twinfoR package look for this global variable. If you give this result a different name, you must manually supply it to the Twitter API methods. Optionally, save this vector in order to skip the authentication process in the future.

dir.create("~/twinfoR_basic",showWarnings=FALSE)
setwd("~/twinfoR_basic")
save(auth.vector,file="auth_vector.RData")

Twitter Search & Status Analyses

This section demonstrates how to search Tweets using the Twitter Search API and analyze the results.

Analyses of Statuses

Most retweeted statuses

Now let’s see which status has been Retweeted the most. It is important to note that these counts are a static snapshot of retweet counts, while the actual Twitter Retweet counts are always changing as users continue to Retweet.

Most Retweeted #Fargo Tweets
Screen_Name Text Retweet_Count
@abc15 Phoenix’s “winter weather” is hard to beat 😎 – #Winter #Brr #Cold #Phoenix #Valley #Fargo #Freeze #Weather #abc15 https://t.co/U4AIDUAtMO 37
@SenJohnHoeven Every year, hundreds of thousands of people come to our nation’s capital to #MarchForLife, including many from #NorthDakota. I met w/ students from #Fargo Shanley HS & #Minot’s Bishop Ryan HS to outline the importance of today’s event & wish them well. #WhyWeMarch https://t.co/owIBeg7UQN 12
@Acevedismos Tal día como hoy, 18/01/2006, Lorne Malvo tuvo un accidente de coche en el pueblo de Bemidji mientras llevaba a una de sus víctimas en el maletero. Después fue al hospital a curarse, conoció a Lester Nygaard y… se lió parda. – #Fargo #Frikimérides https://t.co/6CPB6DIjCY 2
@AgCountryHR It’s not too late to apply to become our next Learning & Development Specialist! Check out the details, here! https://t.co/fzLS4Ez9gb #jobs #Fargo #hiring 1
@AgCountryHR #NowHiring: Farm #Accounting Specialist in our #Fargo office. Full details can be found by clicking here: https://t.co/zEegxDybJ2 #agcareers 1

Analysis of Users

Now we’ll take a look at some basic characteristics of users in the data we’ve collected.

Collecting and Analyzing Users and Timelines

This section demonstrates some methods for collecting Users and User Timelines and analyzing the results.

User search.

Let’s see if we can get some Users from Fargo. We’ll just take the top five of the results.

Collected Fargo, ND Users
User
@FargoNDSchools
@bethhooleVNL
@AlexEganTV
@DowntownFargo
@TEDxFargo

Collect user timelines

Now we collect these users’ timelines. This will take a few minutes because of the Twitter rate limits. This is the first demonstration of one of the API recursive functions, which inserts a delay in each call to prevent exceeding Twitter rate limits. This function, user_timeline_recursive, recursively calls the user_timeline function. See the documentation on these functions for details.

A look at the first user

We’ll look at the first user’s Tweets.

A look at all Users together

Now we’ll combine all of our User’s Tweets and carry out the same analyses.