Jun 7, 2020教程604 words in 4 min


How to acquire Tweets elegantly using R

Twitter as one of the most important social platform could be of value to academical research and development. This article will guide you to acquire Tweets by R and Rtweet based on official API.

Preparation

Step 1

Make sure you have A DEVELOPER ACCOUNT in hand. If not apply one please.🔗Here’s the link

NOTE: Twitter is getting strict about application of new developer account, once your application is refused, your account will be baned(only for developer account).

Use Gmail to sign up an account and choose Explore API when apply could be of great help. Also ,do not select options below like analysis tweets.

Then create a new app if you haven’t have one.

Callback URL must be http://127.0.0.1:1410

Step 2

Go to Dev Environments of Twitter Developer, apply the first two environments.

Choose the app and type a lable. Please do note down the lable.

Step 3

Go to the detail page of your app then switch to **Keys and tokens**

Keep down the four tokens especially the Access token & access token secret.

Step 4

Open your R or RStudio, install Rtweet.

1
2
install.packages('rtweet') #Install Rtweet package
library(rtweet) #Introducing package

Get to work!

Step 5

Authorize your R with tokens in Step 3.

1
2
3
4
5
6
7
8
create_token(
app = "mytwitterapp",
consumer_key = NULL,
consumer_secret = NULL,
access_token = NULL,
access_secret = NULL,
set_renv = TRUE
)

More instruction can be found in doc

Step 6

Now enjoy your ride!

Timeline

1
2
3
get_my_timeline()
get_timeline()
get_timelines()

The three method can be used to acquire timeline of specific account.
Here’s example

1
2
3
4
5
6
7
8
9
10
11
user <- c('microsoft')
timeline <- get_timeline(
user,
n = 100,
max_id = NULL,
home = FALSE,
parse = TRUE,
check = TRUE,
token = NULL,
)
View(timeline)
1
2
3
search_tweets()
search_tweets2()
search_fullarchive()
  • search_tweets() Returns Twitter statuses matching a user provided search query.
  • ONLY RETURNS DATA FROM THE PAST 6-9 DAYS.
  • 3200Tweets Max in this method everytime.
  • To return more than 18,000 statuses in a single call, set “retryonratelimit” to TRUE.

search_tweets2() Passes all arguments to search_tweets. Returns data from one OR MORE search queries.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
q <- "Microsoft or Xbox"
result <- search_tweets(
q,
n = 237,
type = "recent",
include_rts = TRUE,
geocode = NULL,
max_id = NULL,
parse = TRUE,
token = NULL,
retryonratelimit = FALSE,
verbose = TRUE,
)
View(result)

search_fullarchive() can access all history tweets. Free subscription includs 5K Tweets per month.

1
2
3
4
5
6
7
8
9
10
11
12
q <- "from:microsoft" #use from:username to query tweets from specific user
result <- search_fullarchive(
q,
n = 100,
fromDate = 201903010000,
toDate = 201903020000,
env_name = NULL,
safedir = NULL,
parse = TRUE,
token = NULL
)
View(result)

Data Structure

View Data Structure here on offcial Doc.

Export Data

Rtweet offers a method `` helping you export data as CSV fils

1
2
3
4
5
write_as_csv(YourData,
'/path/filename.csv',
prepend_ids = TRUE,
na = "",
fileEncoding = "UTF-8")

Get More

  • Doc is always a friend even though not friendly sometimes XD.
  • View all method of Rtweet here.
  • And offcial API Doc here.

Final Note

  • Please do not try to violate the policy of Twitter or local law.
  • Privacy is important to us all, so take care of the data you get and put them into good use.

Hope you enjoy this ride. Just leave a comment If you run across any problem or notice any mistake in this article.

  • Author:

    WardZhou

  • Copyright:

    Attribution-NonCommercial-NoDerivatives 4.0 International(CC BY-NC-ND 4.0)

  • Updated:

    December 14, 2020

Buy me snacks 🍩.