Getting started with R and R studio (Installation)

Getting Twitter API keys

In order to access Twitter Search or Streaming API, we need to get 4 pieces of information from Twitter: API key, API secret, Access token and Access token secret. You can obtain this information by following the steps below:

1. Extracting tweets with particular hashtag or key word

Once you have obtained the four authentication keys from twitter, you can begin to search for specific keywords of hastags of interest.In the code snippet below, we have extracted two hundred tweets with the keyword “upwork”. These variables can be adjusted as necessary.

#install relevant packages
install.packages("twitteR")
install.packages("ROAuth")
library(twitteR)
library(ROAuth)

#get authentication from Twitter

consumerKey<-   "xxxxxxxxxxxxx"
consumerSecret<-"xxxxxxxxxxxxxx"

accessToken<-"xxxxxxxxx"
accessSecret<-"xxxxxxxxxxxx"

setup_twitter_oauth (consumerKey, consumerSecret, accessToken, accessSecret)  # authenticate

#search for tweets by keyword

tweets<-searchTwitter("upwork", n=200, lang=NULL, since=NULL, until=NULL, locale=NULL, geocode=NULL, sinceID=NULL, maxID=NULL,
                                resultType=NULL, retryOnRateLimit=120)

#Put the tweets in a data frame
tweets<-twListToDF(tweets)

#write out to a csv
write.csv(tweets, file="filename.csv")