I, Rbot: Tweeting from R

[This article was first published on John Baumgartner's Research » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Model status update. A Tweet from the Rbot.

Model status update. A Tweet from the Rbot.

Over the past few weeks I’ve been running batches of JAGS simulations from R. Although these models typically converged within an hour or so, more complex models can take days, or even weeks to converge. Because we, as humans, are required to bathe, feed, socialise and attend weddings, there will inevitably come a time when you are required to leave the safety of your modelling den while your simulation is still chugging away. It would be nice, however, to keep in touch with your models’ progress, to be made instantly aware of any errors that occur in your absence, and to be advised upon successful task completion. This would be decidedly more satisfying than returning from a week-long holiday to find that your model broke on Day 1, just after you closed the door on your way out.

Enter Twidge, a command line Twitter client that humbly fills this neglected niche by allowing you to send a short Tweet to yourself from R via the system() function. The tweet can, of course, be a message pasted together from R objects, which permits dynamic tweet content and means your R-Tweeting power is really only limited by your imagination (and the 140 character message cap).

[EDIT: Since writing this post, I discovered that the twitteR package for R provides a convenient interface to Twitter, and together with the ROAuth package, allows read/write access to your tweets and direct messages. I plan to try out twitteR over the coming weeks, and will report my experiences some time down the track. In the meantime, the following provides an introduction to Twidge as an alternative.]


Unfortunately, Twidge is only available for machines running Linux or Mac OS X. There may be equivalent command line Twitter clients for Windows, but my cursory Googling revealed none that are compatible with Twitter’s recently-altered authentication protocol.

If you’re running Ubuntu, fire up a terminal and run sudo apt-get install twidge. For other Linux distros, head on over to GitHub and grab the source. For Mac OS X, take a look at the additional software installation requirements. Once installed, run twidge setup at your shell prompt, and follow the instructions to authorise Twidge to access your Twitter account.

Basic use:

First things first, tweeting was meant to be easy. Get R up and running and make some functions to streamline your tweets.

tweetdm <- function(x, to) {
  if(nchar(x) > 140) x <- substr(x, 1, 140)
  system(paste("twidge dmsend ", to, " '", x, "'", sep=''))

Now, wherever you want to tweet a message, simply use tweet(x, to), where x is the message (or the object holding the message), and to is the username of the Twitter recipient. The above function sends a direct message (a private message), which requires that the recipient is a ‘follower’ of the Twitter user account you have associated with Twidge (otherwise you will see user error (Bad response: 403)). To send direct messages to yourself, you can either set up two Twitter accounts (a primary account, and a secondary account dedicated to R-tweeting and associated with Twidge), or you can stick with just the one account and send the direct message to yourself.

If you would prefer the whole world to have the opportunity to revel in the glory of your R-tweets (case in point, theRbot’s Twitter feed), use Twidge’s update command instead, which performs a public status update (i.e. a Tweet) rather than a private message. The following function does just that, with an optional at argument, which if used will result in a mention (e.g. @johnbphd) to the specified Twitter user being appended to the end of the tweet. Each of these functions also truncates tweets at 140 characters to prevent Twidge from returning an error.

tweet <- function(x, at=NULL) {
  if(!is.null(at)) at <- paste(' @', at, sep='')
  x <- paste(x, at, sep='')
  if(nchar(x) > 140) x <- substr(x, 1, 140)
  system(paste("twidge update '", x, "'", sep=''))

A simple & tangential example: Saving a plot to Dropbox and Tweeting the URL:

public <- 'http://dl.dropbox.com/u/userid/R_output/'
filename <- 'rbotplot.png'
url <- paste(public, filename, sep='')
message <- paste('File ', filename, ' added to Dropbox. ', url, sep='')
tweetdm(message, 'johnbphd')

In the above example, replace userid in the public folder url string with your unique Dropbox user id number (you can find this by right-clicking on a file in your public Dropbox folder, pointing to the Dropbox context menu item, and selecting ‘Copy Public Link’). The R code saves a plot as a .png into a subdirectory (R_output) within my Dropbox public folder, and sends a tweet (shown below as it appears on my iPhone) with the (shortened) url of the file. (I haven’t accounted for this url-shortening when truncating tweets to 140 characters in the functions above. Be aware of the implications of this rudimentary truncation… one side-effect is that it may lead to a mention inadvertently being made to the wrong Twitter user, e.g. if @johnbphd were truncated to @john.)

Rbot sends me a tweet with a link to a file in my Dropbox.

Rbot sends me a tweet with a link to a file in my Dropbox.

While this example is somewhat pedestrian, it demonstrates the kind of thing that this Twidge-R integration can accomplish. Sure, in many cases you could just use some form of remote desktop connection, but where’s the fun in that? Twidge gives you the peace of mind to enjoy a cheeky beer or catch some fresh air, safe in the knowledge that you will be tweeted in the event of a computational emergency, or — fingers crossed — when R has safely finished the task you’ve set for it.

Stay tuned… the only way forward in this brave new world of cross-platform communication is to eventually control R from Twitter.

Filed under: R, technology Tagged: Dropbox, iPhone, linux, modelling, productivity, R, science, tips&tricks, Twitter

To leave a comment for the author, please follow the link and comment on their blog: John Baumgartner's Research » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)