- R for Data Science Cookbook
- Yu Wei Chiu (David Chiu)
- 522字
- 2021-07-14 10:51:27
Working with twitteR
In addition to obtaining social network interaction data, one can collect millions of tweets from Twitter for further text mining tasks. The method for retrieving data from Twitter is very similar to Facebook. For both social platforms, all we need is an access token to access insight data. After we have retrieved the access token, we can then use twitteR
to access millions of tweets.
Getting ready
In this recipe, you need to prepare your environment with R installed and a computer that can access the Internet.
How to do it…
Perform the following steps to read data from Twitter:
- First, you need to log in to Twitter and access the page of Twitter Apps at https://apps.twitter.com/. Click on Create New App:
Figure 26: Creating a new Twitter app
- Fill in all required application details to create a new application:
Figure 27: Filling in the required details
- Next, you can select Keys and Access Tokens and then access Application Settings:
Figure 28: Copying API key and secret
- Click on the Create my access token button, and the explorer will generate an authorized access token and the secret:
Figure 29: Creating the access token and secret
- Install and load the
twitteR
package:> install.packages("twitteR") > library(twitteR)
- Set up Twitter OAuth with the copied consumer key and consumer secret from Application Settings, and the copied access token and access secret from Your Access Token:
> consumer_key <- '<consumer_key>' > consumer_secret <- '<consumer_secret>' > access_token <- '<access_token>' > access_secret <- '<access_secret>' > setup_twitter_oauth(consumer_key, + consumer_secret, + access_token, + access_secret) [1] "Using direct authentication" Use a local file to cache OAuth access credentials between R sessions? 1: Yes 2: No Selection: 1 Adding .httr-oauth to .gitignore
- At this point, you can use the
searchTwitter
function to extract the top 100 search results withworld cup
as the search key:> res <- searchTwitter("world cup", n=100)
How it works…
In this recipe, we use twitteR
to obtain tweets from Twitter. Similar to Facebook, we need an access token before accessing any tweets. To apply an access token, one must first create an application with one login account and then fill in all the required information to create a new app.
After the app is created, we select the Keys and Access Tokens tab and find both consumer keys and secrets under a section labeled Application Settings. Scroll down further to the button named Create my access token. Clicking on this provides an access token and access token secrets under a section labeled Your Access Token.
At this point, we can now connect Twitter with twitteR
. First, install and load the twitteR
package. You can then copy the consumer key and consumer secret from Application Settings, and the access token and access secret from Your Access Token. This copied information can be used to set up Twitter OAuth. Finally, we can use the searchTwitter
function to find the top 100 search results for the world cup
keyword.
There's more…
Similar to Facebook, Twitter also provides its users a console for API testing. You can access the API console from https://dev.twitter.com/rest/tools/console:

Figure 30: Exploring the Twitter API
- 零基礎PHP學習筆記
- Delphi程序設計基礎:教程、實驗、習題
- Python從入門到精通(精粹版)
- Python程序設計
- HTML5+CSS3+JavaScript Web開發(fā)案例教程(在線實訓版)
- 批調(diào)度與網(wǎng)絡問題的組合算法
- Learning OpenCV 3 Computer Vision with Python(Second Edition)
- 0 bug:C/C++商用工程之道
- Essential C++(中文版)
- 一步一步跟我學Scratch3.0案例
- Python Machine Learning Blueprints:Intuitive data projects you can relate to
- 寫給大家看的Midjourney設計書
- Access數(shù)據(jù)庫應用教程(2010版)
- AngularJS UI Development
- MySQL數(shù)據(jù)庫教程(視頻指導版)