How To Extract Twitter Tweets In R

Mine Twitter Tweets using R

Image for post
Image for post

1. Installed R & RStudio , you can see a video explaining in detail how to do this.
2. A Twitter application and account to extract tweets
3. A Twitter Developers login ID & Password


1. Create a Twitter Application

Sign in using your Twitter Account

Image for post
Image for post

You should’ve been directed to the twitter app. Now click “Create New App

Image for post
Image for post

Create an application

Image for post
Image for post

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

Image for post
Image for post

Go to Keys and Access Tokens

Image for post
Image for post

Write down the Consumer key (API Key) and the Consumer secret (API Secret)

Image for post
Image for post

Create your access token by clicking Create my access token

Image for post
Image for post

Once the access token is created, write down the Access Token and the Access Token Secret

Image for post
Image for post

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:


1) Sentiment Analysis Evaluate Neg and Pos words
2) Twitter Analytics Using R
3) Sentiment Analysis Bernie Sanders
4) Getting Started with Twitter Analysis

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store