Collecting Tweets with R

Jun 14, 2018 00:00 · 327 words · 2 minute read twitter

In this post I am going to download tweets using twitteR package.

  1. To being, you have to have a twitter account. If you do not have, create a twitter account on twitter.com.

  2. Create a twitter application on apps.twitter.com. There, you have to insert a number of information.

  3. Once, you created an application, you will have access to API Key and API Secret in the Keys and Access Tokens tab. In the same tab, you can create access tokens too. Store these information in R, but before that you have to do the step 4.

  4. Go to RStudio: install and attach twitteR package.

install.packages("twitteR")
library(twitteR)
  1. From step 3, store API Key, API Secret, Access Token, and Access Secret into the following code chunk.
api_key <- "YOUR_API_KEY"
api_secret <- "YOUR_API_SECRET"
access_token <- "YOUR_ACCESS_TOKEN"
access_secret <- "YOUR_ACCESS_SECRET"
  1. Now, setup your twitter details with twitteR
setup_twitter_oauth(consumer_key = api_key, 
                    consumer_secret = api_secret, 
                    access_token=access_token, 
                    access_secret=access_secret)
## [1] "Using direct authentication"
  1. Collect tweets of your interest that contains certain text with searchTwitteR() function. For example, I want to collect 500 tweets that used #Afghanistan. That’s simple!
tw_afghanistan <- searchTwitteR(searchString = "#Afghanistan", n = 5)

At this stage, you will have the tweets in the form of a list. You can do any operation that is applicable on lists and character format. For starters, you can print the first tweet.

tw_afghanistan[[1]]
## [1] "jasmin_djokaj: RT @Antiwarcom: 17 Years of War (and More to Come)\nAnniversaries That Never Will Be\nby Tom Engelhardt\n@TomDispatch #Afghanistan\nhttps://t.c…"

Collect tweets from a specific user using userTimeline() function. Here, let’s see what I have tweeted recently.

tw_masood <- userTimeline("masood87r", n = 5)

Let’s see the latest tweet–it appears the tweet is long and it is cut.

tw_masood[[1]]
## [1] "masood87r: The rest of the world, particularly academia, needs to learn from #rstats community. Thanks @StatGarrett for your w… https://t.co/BAvF50OeYJ"

In the next post, I will explore other packages related to twitter and also write about how to do analysis and visualize twitter data.

tweet Share