This is my stab at making a Twitter Bot with R following along with this tutorial from Automated Data Collection with R.You can follow my end product here on Twitter.
First, we’ll need to install and load some packages. We’ll get the most recent version of twitteR
from github.
devtools::install_github("geoffjentry/twitteR")
## Downloading github repo geoffjentry/twitteR@master
## Installing twitteR
## "C:/R-3.1.2/bin/x64/R" --vanilla CMD INSTALL \
## "C:\Users\Mike\AppData\Local\Temp\RtmpgJoXqD\devtools281053ed2576\geoffjentry-twitteR-964f2d0" \
## --library="C:/Users/Mike/Documents/R/win-library/3.1" --install-tests
##
## Reloading installed twitteR
library(twitteR)
library(ROAuth)
Now, we’ll set up some text strings with different adjectives, nouns, and hashtags. These are the raw materials for assembling a new Mexican food.
adjectives1 <- c("Cheesy","Beefy","Spicy","Santa Fe","Tex Mex", "Mexican",
"Stuffed","Crunchy","Grilled",
"Cool Ranch","Spicy Nacho","Creamy","Chipotle","Double","Triple")
adjectives2<- c("Cheesy","Beefy","Spicy","Santa Fe","Tex Mex", "Mexican",
"Stuffed","Crunchy","Grilled","Chicken","Beef",
"Cool Ranch","Spicy Nacho","Creamy","Chipotle","Double","Triple", "Steak",
"Jalapeno")
nouns <- c("Taco","Burrito","Quesadilla","Blaster","Slammer","Slider",
"Popper","Bowl","Gorditta","Nachos","Saltisa","Carnitas","Pizza","Chalupa")
hashtags <- c("#tacos","#TacoTuesday","#Tacos4Life","#tacosauce","#tacotime","#tacosrule")
There are number of different tutorials out there for getting Twitter Authenticated with R
. You will need to create a new App and follow the instructions here or here or here.
api_key <- "Insert_yours_here"
api_secret <- "Insert_yours_here"
access_token <- "Insert_yours_here"
access_token_secret <- "Insert_yours_here"
setup_twitter_oauth(api_key,api_secret,access_token,access_token_secret)
First, we’ll randomly sample the number of nouns and adjectives to have in the tweet.
numb_adjectives1 <- sample(seq(0,2,1),size=1)
numb_adjectives2 <- sample(seq(0,1,1),size=1)
numb_nouns <- sample(seq(1,2,1),size=1)
Next, we’ll go ahead and sample that number.
# randomly choose your first adjectives
random_adjectives1 <- NULL
for(i in 1:numb_adjectives1){
random_adjectives1 <- c(random_adjectives1,sample(adjectives1,size=1))
}
# randomly choose your second adjectives
random_adjectives2 <- NULL
for(i in 1:numb_adjectives2){
random_adjectives2 <- c(random_adjectives2,sample(adjectives2,size=1))
}
# randomly choose your nouns
random_nouns <- NULL
for(i in 1:numb_nouns){
random_nouns <- c(random_nouns,sample(nouns,size=1))
}
# randomly choose a hashtag
random_hashtag <- sample(hashtags,size=1)
Now, fix any instances where you’ve sampled the same noun twice
if(length(random_nouns)==2){
if(random_nouns[1]==random_nouns[2]){
random_nouns <- random_nouns[1]
}
}
Finally, combine all of those elements into one string:
temp <- c(random_adjectives1,random_nouns,random_hashtag)
tweettxt <- paste(temp,collapse=" ")
tweettxt
## [1] "Cheesy Blaster #tacos"
This is all it takes to send out the tweet
tweet(tweettxt)
And we can even keep a record of all of our tweets
line <- paste( as.character(Sys.time()), tweettxt,sep="\t" )
write(line, file="tweets.log", append=TRUE)
Now, this thing needs to get automated. Since I’m running Windows, we can use Task Scheduler.
Open up Task Scheduler. Click on Action, Create Task and give your task a name:
Then, under Triggers pick your scheduling for the tweet. For example, you can make it daily, weekly, or monthly. You can also repeat the task every few minutes or hourly.
Finally, under Actions you need to locate the file Rscript.exe for your installation of R
. Mine was under “C:-3.1.2 bin x64 Rscript.exe”. For Add Arguments choose the name of the R
script where all of this code is saved. Finally, for Start in specify the directory for that script.
Thanks again to Simon Munzert (@simonsaysnothin) at Automated Data Collection with R for this tutorial.