Accessing Data from Twitter API using R

Michael Galarnyk
2 min readJan 24, 2017

--

UPDATE: An updated version of this tutorial can be found here.

Using the Twitter API should be an easy thing, but sometimes pictures and simple code can save you that extra 5 minutes. With that, this post goes over how to use the Twitter API to get tweets using R.

First we need Twitter API Access. The following steps will help you set up your twitter account and be able to use the Twitter API.

  1. Create a twitter account if you do not already have one.
  2. Go to https://dev.twitter.com/apps and log in with your twitter credentials.

3. Click “Create New App”

Click on Create New App.

4. Fill out the form and agree to the terms. Click on Create your Twitter application. Put your Callback URL as http://localhost:1410.

Create your Twitter application

5. On the next page, click the “Keys and Access Tokens” tab along the top, then scroll all the way down until you see the section “Your Access Token”

Click on Keys and Access Tokens

6. Click the button “Create My Access Token”.

Click on Create my access token

7. You should have a screen like the following below.

FYI Dont share your keys or access token. These keys and tokens used in this tutorial no longer exist.

8. Finally we actually get to use the API to get data. Using the following code below, we can get twitter data.

The twitteR package makes accessing data very easy

Note: The reason why we supply the access_token and access_secret is described in the setup_twitter_oauth .Rd file.

Please let me know if you have any questions! You can either leave a comment here, on youtube or Twitter.

--

--