Twitter API in R

Twitter allows developers to gather a limited number of Tweets for data analysis.

To use the Twitter API, you will need an ACCOUNT and 4 (FOUR) passcodes 🙂 Yep – 4!

Once you have this, you can use the following R code to access Tweet data. Notice that you can choose the number of Tweets and the date (if you wish). Read the Twitter Documentation to learn the constraints.

##################################
## The Twitter API in R
##################################
## Gates
#install.packages("selectr")
#install.packages("rvest")
#install.packages("xml2")

library("selectr")
library("rvest")
library("xml2")

BehindDrG<-read_html("http://drgates.georgetown.domains/")
print(BehindDrG)

JustText <- html_text(BehindDrG)
print(JustText)

### Fun Reference
## https://www.freecodecamp.org/news/an-introduction-to-web-scraping-using-r-40284110c848/


##-------------------------------------------------------------
#############################################################
### The Twitter API   ########################################
############################################################
## You MUST first apply for and get a Twitter Dev Account
## Create a new App on the account AND
## get the access codes
## https://developer.twitter.com/en/apply-for-access.html
##############################################################

#install.packages("twitteR")
#install.packages("ROAuth")
#install.packages("rtweet")
library(rtweet)
library(twitteR)
library(ROAuth)
library(jsonlite)

setwd("C:/Users/profa/Documents/R/RStudioFolder_1/DCR2019Networks/")
## The above is where my Twitter Dev passcodes are located.
## This will NOT BE THE SAME FOR YOU

## What is going on here?
## Here - I have placed the 4 Twitter passcodes into a .txt file. When I need them, I read them out
## of the file. This is better and safer than coding them in directly. 
filename=filename="C:/Users/profa/Documents/RStudioFolder_1/DCR2019Networks/TwitterConKey_ConSec_AccTok_AccSec.txt"
(tokens<-read.csv(filename, header=TRUE, sep=","))

## This is important. You need to assure that your codes read in as character strings.
(consumerKey=as.character(tokens$consumerKey))  ## tokens is what I named my passcodes read from my file.
consumerSecret=as.character(tokens$consumerSecret)
access_Token=as.character(tokens$access_Token)
access_Secret=as.character(tokens$access_Secret)

## Do not remove this
requestURL='https://api.twitter.com/oauth/request_token'
accessURL='https://api.twitter.com/oauth/access_token'
authURL='https://api.twitter.com/oauth/authorize'

## Set up  - log in - to Twitter
setup_twitter_oauth(consumerKey,consumerSecret,access_Token,access_Secret)
## Use the twitteR library, method searchTwitter to search a specific hashtag, number of tweets
## I am getting three here, and the date (optional). 
## DO NOT get too many Tweets at once while you are testing your code or YOU WILL RUN OUT FOR THE DAY
## Twitter LIMITS the number of Tweets you can grab per day and at once.
Search1<-twitteR::searchTwitter("#Gators",n=3, since="2020-03-01")
(Search_DF2 <- twListToDF(Search1))

(Search_DF2$text[1])


########## Place Tweets in a new file ###################
FName = "MyFileExample.txt"
## Start the file
MyFile <- file(FName)
## Write Tweets to file
cat(unlist(Search_DF2), " ", file=MyFile, sep="\n")
close(MyFile)