Time for a Tweetstorm?

140 characters is hard

So-called “tweetstorms” – threaded tweets on the same topic – are getting pretty common on Twitter. 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!

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:1

tweetstorm <- function(s) {

  s1 <- paste(strwrap(s,134), collapse="\n")
  
  s2 <- strsplit(s1, "\n")
  
  numlines <- length(s2[[1]])
  index <- 1:numlines
  
  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])
  
  return (cat(storm))
}

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("Do you know about Project CRediT? They've developed a set of contributor tags to be used as meta-data attached to every article. If widely adopted as a standard at journals, all contributors to a manuscript, **even those who aren't authors**, will get credit for their contribution. It's brilliant. See https://goo.gl/NRfbdt")

(Of course, you’ll replace the text above with your own tweetstorm.) Once you do that, here’s the outcome:

Do you know about Project CRediT? They've developed a set of contributor tags to be used as meta-data attached to every article. If 1/3

  widely adopted as a standard at journals, all contributors to a manuscript, **even those who aren't authors**, will get credit for 2/3
 
  their contribution. It's brilliant. See https://goo.gl/NRfbdt 3/3

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:

Cool, right? Give it a try and let me know how useful it is and how you would make the function better.


  1. I’m not a programmer. My guess is this code is uglier and slower than it needs to be. So be it.

 

Leave a Reply