R Ready to Map Unit 1: Collecting Twitter Data

Dorris Scott
3/28/2018

Unit Objectives

  • To understand the Twitter API used for collecting data.
  • To learn how to do a one-time collection of tweets.
  • To learn how to do a continuous collection of tweets.
  • To generate a collection of tweets on a topic of your choice.

First Things First!

Install the rtweet package and load it.

install.packages("rtweet")
library(rtweet)

If you are getting error messages about packages missing then:

install.packages("rtweet", dependencies = TRUE)
library(rtweet)

Lesson 1: Understanding the Twitter API

What is Leaflet?

Lesson 1: Understanding the Twitter API

Make sure to take note of these things when you're making your Twitter app!

  • App Name
  • API Key
  • API Secret

Lesson 2: Doing a One-Time Collection of Tweets

##whatever name you assigned to your created app
appname <- ""

##api key 
key <- ""

##api secret
secret <- ""

##create token named "twitter_token"
twitter_token <- create_token(
  app = appname, 
  consumer_key = key,
  consumer_secret = secret)

Lesson 2: Doing a One-Time Collection of Tweets

FMTweets <- search_tweets("farmers market", n = 1000, include_rts = FALSE, geocode = lookup_coords("georgia"))

Lesson 2: Doing a One-Time Collection of Tweets

Lesson 3: Doing a Continuous Collection of Tweets

Let's see who's tweeting about lunch right now!

Tip: Choose the appropriate meal for the appropriate time of day ;)

t<- 60 * 5
lunchstream <- stream_tweets("lunch", timeout = t)

Lesson 3: Doing a Continuous Collection of Tweets

t <- 30 means to search for tweets for 30 seconds.

t <- 60 * 3 means to search for tweets for three minutes.

t <- 60 * 60 * 24 means to search for tweets for a period of one day.

Lesson 4: It's Your Turn!

Try doing a Twitter search about breakfast, lunch, or dinner!

  • Compare and contrast the results when using the stream_tweets and the search_tweets functions.