Fall 2017

Twitter API

  • Twitter is a popular service that allows us to broadcast short messages, \(tweets\)
  • Cultivation of open API development represents another level of evolution in Internet participation. Not just reading and writing content, but co-creating interactions around that content
  • Twitter has a low barrier for both, we're going to learn how to get started with working with the API in R and go through an example of how parts of the API can be assembled into web applications to build your own tools

Getting Started

Twitter App Created

  • Click the "Keys and Access Tokens" tab, and scroll down to "Your Access Token"

  • Select "Create My Access Token" and copy the following into R: Consumer Key, Consumer Secret, Access Token, and Access Token Secret

 # Load packages -----------------------------------------------------------
library(twitteR)

# Twitter Creds -----------------------------------------------------------
consumer_key <- ""
consumer_secret <- ""
access_token <- ""
access_secret <- ""

# Set up ------------------------------------------------------------------
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)

Exploring Twitter

  • \(\texttt{searchTwitter}\): Search for tweets that match a desired term. The \(\texttt{n}\) argument specifies the number of tweets to return, but we're generally limited to the last weeks worth of tweets
  • \(\texttt{strip_retweets}\): Takes a list of status objects and remove official retweets. When "RT/MT @username" was used versus the retweet button, you can add the option of \(\texttt{strip_manual=TRUE}\).
tweets <- searchTwitter("#rladies", n = 200, retryOnRateLimit = 120)
stripped_rt <- strip_retweets(tweets, strip_manual = TRUE, strip_mt = TRUE)

Looking @ Users

  • \(\texttt{getUser}\): Take a closer look at a specific user
  • \(\texttt{str(user)}\): We can poll the \(\texttt{user}\) object for further information
joyce <- getUser('joyceisms')
str(joyce)
joyce$getDescription()
joyce$getFriends(n = 5)
joyce$getFavorites(n = 5)

Checking Timelines

The \(\texttt{twitterR}\) package supports two kinds of timelines, \(user\) and \(home\). The \(\texttt{userTimeline}\) provides the most recent tweets of a specific user, while \(\texttt{homeTimeline}\) provides your own.

rladies_tweets <- userTimeline("rladiesRTP")
my_tweets <- homeTimeline()

Get Trends

Conversion to data.frames

We know how to work with lists via purrr! We can also convert to data frames. To do this, every class has a reference method \(\texttt{toDataFrame}\) as well as corresponding \(\texttt{S4}\) method \(\texttt{as.data.frame}\). There is also a convenience method:

df <- twListToDF(tweets)
View(df)

Let's Visualize

Latest 100 #Thanksgiving Tweets

Database Persistence

We have to pay to retrieve Twitter data in the past, but if we have the ability to look ahead, Twitter API can enable prospective studies. We do this by collecting data and automatically persisting it to a database.

library(bigrquery)
insert_upload_job("prac-dataviz", "rladies", "df", df)

More on using Google BigQuery here.

Using twitteR

References