Previously I have spoken on the merits of the R programming language (here) and alluded to its place in my research (here)[1], so today I thought it was about time to share how I approach using R in my research. R–just like any other tool–needs to be leveraged in such a way that the use of it doesn’t cost more than the benefit it provides. For example, if I try to write a script to compile a test program which I’m only going to use once, then my time is probably better spent in other pursuits. Now if I plan on compiling and running that program a number of times under a variety of conditions then a script may be well suited, like the way I did here.
Scripting is the oft-neglected chapter in the language tutorial that will save your life. I know just from my own experience that trying to learn in a formal sense[2] about writing and implementing scripts is hopeless. It’s hard to sink your teeth into something that has no context, and scripting text usually has no context. Instead I learn as I go and search for each aspect of my solution independently of the others–think line by line. Nevertheless, being able to write scripts for aspects of a project that you find yourself going back to again and again is worth its weight in gold and will save you so much time.
For an example, consider this stupidly simple script I wrote for my inverse modeling work.
#/bin/bash Rscript MCMC.r 1 1000000 1000 1000000 1.5 & Rscript MCMC.r 2 1000000 1000 1000000 1.5 & Rscript MCMC.r 3 1000000 1000 1000000 1.5 & Rscript MCMC.r 4 1000000 1000 1000000 1.5 & wait echo "=================" echo "======Done=======" echo "================="
Now this script has just four ‘functional’ lines of code and the rest is fluff such as giving me an aesthetically pleasing Done statement. This script allows me to readily test a large number of different conditions and initializations for my model without having to manually run each of them (or to keep track of them). As an added benefit, it also allows me to run them simultaneously (R inherited S’s lack of multithreading).
Now to move onto the Tweeting part of this article. In my last version of my modeling program, I decided to make the program notify me of its completion via Twitter. That way when I leave the program running for hours on end I know when I should head back to my computer and check the results. In addition I also have the program send me the compiled images that result from the model.
Now I am certainly not saying that every program should live tweet its progress to you[3], but being able to efficiently monitor a program is essential if you’re going to be working intimately with it like I am now. It not only saves you time when checking for results, but it also improves your relation and workflow surrounding the program. A little novelty certainly never hurt anyone when you work so many hours on a project.
In case there are a few out there that would like to implement a tweeting R code then take a look at the following[4].
## Load the required libraries library("twitteR") library("digest") library("RCurl") ## Set up the authentication consumer_key <- 'your key' consumer_secret <- 'your secret' access_token <- 'your token' access_secret <- 'your secret' setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret) ## Setup custom functions to make use of twitter and ftp up = function(name,type,cycle) { newname = paste(digest(algo="crc32", paste(as.character(Sys.time()),name)),type, sep="") ftpUpload(name, paste("ftp://username:password@domain",newname, sep="")) message = paste("@TbryceKelly. Your results of ",name, " (", cycle, ") are now online: http://misc.tkelly.org/R/",newname,sep="") tweet(message) }
On the whole, it’s nice to know these small tips and tricks when setting up your own packages. Even if it doesn’t ultimately help, at least it makes the programming a bit more fun (and that is never a bad thing).
Notes
- I also showed how I used it with some of my excursions into Computational Fluid Dynamics here and previously here.
- “Formal” as in textbook, lessons, or even step by step tutorials. This is in opposition to what I would describe as “informal” where StackExchange, forums, code snippets play the primary role in the education.
- A life tweeting program is pretty awesome though.
- I never said it is a very secure implementation.
One thought on “Making R your own: Scripting & Tweeting”
Comments are closed.