Mine Twitter Tweets using R
1. Create a Twitter Application
Sign in using your Twitter Account
You should’ve been directed to the twitter app. Now click “Create New App”
Create an application
NOTE: If you haven’t already, then you may need to add your phone number to your twitter profile. Go to settings and privacy→ Mobile
Go to Keys and Access Tokens
Write down the Consumer key (API Key) and the Consumer secret (API Secret)
Create your access token by clicking Create my access token
Once the access token is created, write down the Access Token and the Access Token Secret
2. Install and Load R packages
Open RStudio or whatever IDE you are using for R, and install and load the following two packages. If you need an IDE here is a video showing how to install RStudio. If you don’t know how to install packages on RStudio, here is a video on how to do that as well here.
twitteR: Provides an interface to the Twitter web API.
#Install and Load R packagesinstall.packages("twitteR")
3. Setup Twitter Authentication
Use your consumer key, consumer secret, access token, and access secret that you wrote down in the earlier steps from the above, and replace ‘xxxxxxxxxxx’ with the appropriate values.
4. Extract Tweets
The searchTwitter function code below will return 3 tweets about the nba in english.
n= the maximum number of tweets to return
lang = restrict tweets to the given language
SearchString = “nba”
searchTwitter("nba", n=3, lang="en")
Get the R code here on my GitHub: