R number generation

On her blog, Christina Bergey has previously alluded to our discussions about number generation. I am posting my solution here, not because it is the best solution, but because it shows how R code can be written into Unix scripts (see also this previous post).

# Arguments = vector -n digits -p prefix -s suffix



while getopts n:p:s: opt; do
	case $opt in
		n) digits=$OPTARG;;
		p) prefix=$OPTARG;;
		s) suffix=$OPTARG;;

echo "cat(paste('$prefix',formatC($vector,width=$digits,
	format='d',flag='0'),'$suffix',sep=''),sep='\\n')" | 
	/usr/bin/R --quiet --vanilla --slave


Now suppose you want to utilize the above number generation script to rename image files. Your current directory contains exactly ten .jpg images:


You can rename the files (or rather generate the Unix code for renaming them) using the paste utility and some process substitution. Example below (assumes that num is the name of the Unix script):

paste -d '\ ' <(num 'rep("mv",10)') <(ls -1 *.jpg) <(num 1:10 -p dog -s .jpg)

This outputs:

mv dog.jpg dog01.jpg
mv dog_and_me.jpg dog02.jpg
mv dog_at_beach.jpg dog03.jpg
mv dog_in_house.jpg dog04.jpg
mv dog_in_park.jpg dog05.jpg
mv dog_sleeping.jpg dog06.jpg
mv dog_sleeping_super_cute.jpg dog07.jpg
mv dog_sleeping_super_fuzzy.jpg dog08.jpg
mv dog_sneezing.jpg dog09.jpg
mv dog_yawning.jpg dog10.jpg

Running these commands in Terminal (from the proper directory) will rename all ten images. Your directory listing is now this:


As a commenter pointed out in a previous post, R can run directly from Terminal. An R session, however, runs separately from other processes and the work performed in a session cannot be passed along to other programs without the intermediate step of writing files. Here, I show how R code can be written into Unix scripts without the need to start an R session.

Posted in Productivity | Tagged , | Leave a comment


Recently, I attended a talk by my colleague Mike Montague with the Warren Lab at the Genome Institute at Washington University in which he presented several ongoing projects, including the sequencing of cephalopod genomes. Cephalopods include octopus, squid, and cuttlefish and have fascinating sensory adaptations. Many animals are adept at camouflage but few do so as spectacularly and with as much cognitive effort as these creatures.

The videos below include footage of cephalopods from research by the Hanlon Lab at the Marine Biological Laboratory. For more short clips of wild cephalopods and lab experiments, also check out Roger Hanlon’s YouTube channel.

Update: They are cephalopods, not “cephalapods.” Thanks to Dr. Maja for pointing this out!

Posted in Wildlife | Tagged , , , , | Leave a comment

R calculator in Terminal

Until recently, the Mac Calculator app was on my Dock (Mac’s version of a Start menu). It bugged me tremendously that an entire slot was occupied by a program much harder to use than my 15 year-old TI-34 scientific calculator.

TI-34: still the better calculator

But before removing Calculator from the Dock, I had to make sure its functionality was covered by another program on the Dock. Chrome was an option—Google Search can do standard arithmetic and trigonometry (example) while Wolfram Alpha can do more and with greater flexibility (example). R is also an option.

But for me, both of these are not ideal. I already struggle with the number of open tabs in my browser and it annoys me to startup the R GUI just to do a quick calculation. Since I always have Terminal open, I instead wrote a quick bash script to run R commands from Terminal:



echo "cat($input,\"\n\")" | /usr/bin/R --quiet --vanilla --slave

To run, save to your bin folder, then run as a bash script (will work only for Mac and Linux users). The script takes a single R command as its argument (given in quotes or with special characters escaped). For instance, if you saved the script as c (for calculator), then run:

c "12 * 6"
# returns 72
Posted in Productivity | Tagged , , | 5 Comments


The impala: a common antelope but with tremendous grace and speed and one of my favorite African mammals.

ARKive video - Impala pronking and leaping

Posted in Wildlife | Tagged , , | Leave a comment

Central American primate distributions

I was recently cleaning out files on my computer when I stumbled across some maps that I had created two years ago for a class presentation. These maps show the species distributions for all Central American primates, and they are in vector format so they are high-quality and easy to modify. I have no plans for these images, so I decided to release them here.

Species distribution maps were modified from:
Ford, S.M. 2006. The biogeographic history of Mesoamerican primates, in: Estrada, A., Garber, P.A., Pavelka, M., Luecke, L. (Eds.), New Perspectives in the Study of Mesoamerican Primates. Springer, New York, pp. 81–114.

A PDF version is available for download here. Import into a vector editing program such as Adobe Illustrator or Inkscape to view and modify the species distributions. I also played around with SVG and JavaScript and made the map below interactive. Enjoy!

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

Select a genus
Saimiri Cebus Aotus Saguinus Ateles Alouatta Clear

Posted in Maps, Primates | Tagged , , , | 1 Comment