Experiences of coding a Twitter bot in R

Tumuhimbise Moses
Jan 31, 2018 · 3 min read

In searching for ways how to maximize data analysis capabilities in social media analytics offered by R particularly how to scrap data from Twitter, I stumbled upon a post on how to create a Twitter Bot in R by Automated Data Collection with R. Inquisitive to put this into action, I rolled my sleeves and delved into the challenge.

I crafted my Bot according to the author’s Bot which motivates PhD students to return to work by barking orders using mild insults.

In the same suit, I created three Microsoft Excel workbooks to act as database storage for phrases.

The first step was to install the XLConnect library that allows Excel worksheets support for R based script. This was followed by loading the MS Excel sheets and printing the stored phrases to ensure the XLConnect library functions.

The first bug I encounter reveals an output error of an unused argument within the XLConnect library (the unused argument being simply=T). After a few minutes, I realize it is typing error, instead of using the correct arguments simplify, I wrote simply.

After solving the typo error, the script is able to print out the phrases from our MS Excel sheets. The next step is to repeat the same procedure and print out words and insults from other MS Excel sheets. In essence, the Bot is a troll Bot aimed at tweeting trolling tweets which are somewhat insulting. We then combine our phrases to make clear sentences with help from the stringr package.

The next stage involves connecting to the Twitter API which proved to be the most challenging section of the script. All errors point to an error in my Oauth authentication setup.


Yet another error!!

Is there an end to this ??

Error!! Error!!

After much deliberation, I had to make small changes to the code such as removing certain arguments and unnecessary parentheses as well additional packages such as XML and ROauth.

Finally the script was able to send a tweet.

Take note of the comparisons below to observe the difference between the earlier and recent versions of the script.

First version
Second version

This is the complete working code.

Tumuhimbise Moses

Written by

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade