R: Connect to Twitter with R

You can do a lot in the way of text analytics with Twitter. But in order to do so, first you need to connect with Twitter.

In order to do so, first you need to set up an account on Twitter and get an API key. Once you have created your account, go to the following website: https://dev.twitter.com/

Once there, click My apps

twiiter

Click Create New App

twitter1

Give name, description (it doesn’t matter – matter anything up), for website I just used http://test.de

twiiter2

Go to Keys and Access Tokens

twitter3

Get your Consumer Key (API Key), Consumer Secret (API Secret), Access Token, and Access Token Secret

Now open R and install the TwitteR package.

install.packages("twitteR")

Now load the library

library(twitteR)

Next we are going to use the API keys you collected to authorize our connection to the Twitter API (Twitter uses OAuth by the way)

api_key<- "insert consumer key here"
api_secret <- "insert consumer secret here"
access_token <- "insert access token here"
access_token_secret <- "insert access token secret here
setup_twitter_oauth(api_key,api_secret,access_token,access_token_secret)

Now to test to see if your connection is good

searchTwitter('analytics')

5 thoughts on “R: Connect to Twitter with R

  1. Pingback: R: Twitter Sentiment Analysis – Analytics4All

    1. Samantha, sorry for the late reply. I just got home from work. If you are getting an error stating you cannot find function searchTwitter(), it must have something to do with the install of twitteR. The first question I have is are you running R from CRAN or are you using another version like Open R from Microsoft (not all versions support all R libraries). If you are running R from CRAN, I would suggest uninstalling the twitteR a library and reinstalling it. I’d throw in a reboot in between for good measure. The sad reality is, programmers spend wayyyy too much time troubleshooting problems.

Leave a Reply