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 behttp://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 | install.packages('rtweet') #Install Rtweet package |
Get to work!
Step 5
Authorize your R with tokens in Step 3.
1 | create_token( |
More instruction can be found in doc
Step 6
Now enjoy your ride!
Timeline
1 | get_my_timeline() |
The three method can be used to acquire timeline of specific account.
Here’s example
1 | user <- c('microsoft') |
Search
1 | search_tweets() |
search_tweets()
Returns Twitter statuses matching a user provided search query.- ONLY RETURNS DATA FROM THE PAST 6-9 DAYS.
3200
Tweets 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 | q <- "Microsoft or Xbox" |
search_fullarchive()
can access all history tweets. Free subscription includs 5K Tweets
per month.
1 | q <- "from:microsoft" #use from:username to query tweets from specific user |
Data Structure
View Data Structure here on offcial Doc.
Export Data
Rtweet offers a method `` helping you export data as CSV fils
1 | write_as_csv(YourData, |
Get More
Doc link
- 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.