STEP 1: Load the rtweet and other needed R packages.

Note you are introducing 2 new packages lower in this lesson: igraph and ggraph.

# load twitter library - the rtweet library is recommended now over twitteR
library(rtweet)
# plotting and pipes - tidyverse!
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# text mining library
library(tidytext)
# plotting packages
library(igraph)
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:dplyr':
## 
##     as_data_frame, groups, union
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
library(ggraph)

Create a twitter developer account

In order to download tweets, you will need a twitter account.

Then you will need a developer account. Follow this CRAN tutorial in order to create a developer account: https://cran.r-project.org/web/packages/rtweet/vignettes/auth.html

As an example, my developer account is: MC data mining. You basically need to provide twitter with a reason that you would like this account, and obviously, it is to learn to mine tweets for your data science class. It is typically a fast approval process.

Once you have created a developer app, you are ready to start.

Authorization Methods

I suggest you select option #2, which I will show below:

Access Token/Secret Method

Copy and paste the four keys (along with the name of your app) into an R script file and pass them along to create_token().

store api keys (these are fake example values; replace with your own keys)

api_key <- “…” api_secret_key <- “…” access_token <- “…” access_token_secret <- “…”

#api_key <- "..."
#api_secret_key <- "..."
#access_token <- "..."
#access_token_secret <- "..."


api_key <- "onsqqdn6FkeFG2jeFLeODn6UE"
api_secret_key <- "50AZmbOZizq29Hiiy5a3kheDl93Z3ym5lWTxZLywLLGuswvX3b"
access_token <- "432673917-rOQ8W1Wgi1qAH1NeS3BbNByogRlrhq34kzqWgTrd"
access_token_secret <- "OgGb43o5Rwv5nGQvuocbHmkLVH7imPQsIUfzBjxQpxVoK"

## authenticate via web browser
token <- create_token(
  app = "MC data minig",
  consumer_key = api_key,
  consumer_secret = api_secret_key,
  access_token = access_token,
  access_secret = access_token_secret)

Authorization in future R sessions

The create_token() function should automatically save your token as an environment variable for you. So next time you start an R session [on the same machine], rtweet should automatically find your token.

Now you can start mining for tweets!