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
## "C:/R-3.1.2/bin/x64/R" --vanilla CMD INSTALL  \
##   --library="C:/Users/Mike/Documents/R/win-library/3.1" --install-tests
##
## Reloading installed twitteR
library(twitteR)
library(ROAuth)

# Set-up text bits

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")

"Stuffed","Crunchy","Grilled","Chicken","Beef",
"Cool Ranch","Spicy Nacho","Creamy","Chipotle","Double","Triple", "Steak",
"Jalapeno")

"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)

# Generate the tweet

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_nouns <- sample(seq(1,2,1),size=1)

Next, we’ll go ahead and sample that number.

# randomly choose your first adjectives
}

}

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"

# Send the tweet

This is all it takes to send out the tweet

tweet(tweettxt)

# Create log entry

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)

# Automating the tweets

Now, this thing needs to get automated. Since I’m running Windows, we can use Task Scheduler.

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.