140 characters is hard
There are occasions when you might want to respond to someone using more than 140 characters on Twitter. You’re not alone. So-called “tweetstorms” – threaded tweets on the same topic – are getting pretty common. In fact, they’re common enough that clever people such as Yana Weinsten (@doctorwhy) created an Excel file to make it easier to simply type out your thoughts without simultaneously thinking through how to break them up into 140-character chunks. Brilliant!
Hello Twitter, I accidentally made a spreadsheet to help people go on Twitter rants. This is my first tweet in the rant. 1/6 pic.twitter.com/XgC3hta0Jo
— doctorwhy (@doctorwhy) June 17, 2017
This made me wonder about implementing something similar in R, hence this post.
An R approach
I started using R in earnest this summer. It’s amazing for so many reasons that I’ll maybe blog about another time. I’m still no expert, but I’ve learned enough about the fundamentals of R to write simple functions. In this case, I wrote a function that allows a user to generate a tweetstorm based on a block of text. Maybe you’ll find it useful.
Here’s the function:[I’m not a programmer. My guess is this code is uglier and slower than it needs to be. So be it.]
# if tweetnow is set to TRUE, sends the tweets directly from R to Twitter
# this function requires installing the rtweet package and setting up the relevant
# Twitter user credentials
# otherwise, the tweetstorm outputs to the console; the user can copy/paste to Twitter manually
tweetstorm <- function(s, tweetnow = FALSE) {
#if you'll be tweeting directly from R, update the user name and token variables
# (no need to update these variables if you won't be tweeting directly from R)
self_user_name <- "YourTwitterHandle"
token <- "YourAccessToken"
#################################################################
s1 <- paste(strwrap(s,134), collapse="\n")
s2 <- strsplit(s1, "\n")
numlines <- length(s2[[1]])
index <- 1:numlines
if(tweetnow==FALSE){
counter <- paste(" ",index,"/",numlines,"\n\n")
counter <- gsub(" ","",counter)
counter <- gsub("n/"," n/",counter)
storm <- paste(strwrap(s2[[1]][index],140-max(index)+2+nchar(max(index))), counter[index])
cat(storm)
}
if(tweetnow==TRUE){
counter <- paste(" ",index,"/",numlines)
counter <- gsub(" ","",counter)
counter <- gsub("n/"," n/",counter)
storm <- paste(strwrap(s2[[1]][index],140-max(index)+2+nchar(max(index))), counter[index])
library(rtweet)
post_tweet(storm[1])
for (i in 2:length(storm)){
my_timeline <- get_timeline(self_user_name, n=1)
reply_id <- my_timeline[1,]$status_id
post_tweet(status=storm[[i]], in_reply_to_status_id=reply_id)
}
}
}
To use this function, copy the above code and paste it into a text file called “tweetstorm.R”. Open tweetstorm.R in R and run the code. Once you’ve done that, simply type your tweetstorm into your R console like so:
tweetstorm("Update! If you're reading this, that means that my silly tweetstorm function in R has been updated successfully to allow the user to post their tweetstorm directly from R instead of having to copy and paste. AND IT'S THREADING PROPERLY TOO. Great idea generated by, @AonghaisC! https://twitter.com/AonghaisC/status/900115226623979525 What a world. I really hope the machines and/or zombies don't take over too soon; I'd like to enjoy it a little bit longer.",tweetnow=FALSE)
(Of course, you’ll replace the text above with your own tweetstorm.) Notice that I specified the “tweetnow” argument to be FALSE (the default). What that means is the tweetstorm will show up in your R console like this:
## Update! If you're reading this, that means that my silly tweetstorm function in R has been updated successfully to allow the user to 1/4 ## ## post their tweetstorm directly from R instead of having to copy and paste. AND IT'S THREADING PROPERLY TOO. Great idea generated by, 2/4 ## ## @AonghaisC! https://twitter.com/AonghaisC/status/900115226623979525 What a world. I really hope the machines and/or zombies don't 3/4 ## ## take over too soon; I'd like to enjoy it a little bit longer. 4/4
All you need to do is copy each chunk of text and paste it into a successive set of tweets. If you reply to your own last tweet each time, the points you want to make will be threaded properly. Here’s what the above looks like on Twitter:
Update! If you're reading this, that means that my silly tweetstorm function in R has been updated successfully to allow the user to 1/4
— Heather Urry (@HeatherUrry) August 23, 2017
And if you want to post your tweetstorm directly from R, you can do that too. First you need to authorize your Twitter account. I found Michael Kearney’s instructions at https://github.com/mkearney/rtweet really helpful. Nota bene: At the time of this writing, I had to install the development version of the rtweet package to enable the threading we want to achieve.
Once that’s in place, enter your tweetstorm as described above, this time setting the tweetnow argument to “TRUE”.
library(rtweet)
tweetstorm("Update! If you're reading this, that means that my silly tweetstorm function in R has been updated successfully to allow the user to post their tweetstorm directly from R instead of having to copy and paste. AND IT'S THREADING PROPERLY TOO. Great idea generated by, @AonghaisC! https://twitter.com/AonghaisC/status/900115226623979525 What a world. I really hope the machines and/or zombies don't take over too soon; I'd like to enjoy it a little bit longer.",tweetnow=TRUE)
Once you hit enter, the tweets will be posted as a thread.
Cool, right? Give it a try and let me know how useful it is and how you would make the function better.
Post updated: 22 August 2017