TextWrangler and R
January 10, 2009 - 18:53
R is an environment for statistical computing (see also ‘Data Analysts Captivated by R's Power’ in the New York Times) which I use on an almost daily basis. However, the included text editor is not all that great, and that is why I use TextWrangler to edit my R scripts. To get the most out of this combination, I searched the internet for syntax highlighting and for an apple script for executing my code directly from TextWrangler in R. It took me a while to find it, especially the script that only executes the selection in TextWrangler (as opposed to the complete file). This is the best I could find:
- the syntax highlighting file. Save this file in ~/Library/Application Support/TextWrangler/Language Modules/ (make sure you keep the ‘.plist’ extension), and restart TextWrangler. Go to TextWrangler’s preferences -> Languages, and add a new suffix mapping (e.g., .r to R language).
- an applescript to execute the selection in TextWrangler. Put this in ~/Library/Application Support/TextWrangler/Scripts/. You can assign a shortcut by going to the Window menu in TextWrangler, then choose Palettes -> Scripts and assign a shortcut, for instance cmd-return for the same behavior as the built-in R text editor. It either executes your current selection, or the line on which your cursor is. Note: for some versions of TextWrangler, you will have to rename the applescript from .txt to .scpt for TextWrangler to recognize the file (it turns out that you sometimes have to save the file with ‘script editor’, to get TextWrangler to recognize it, thanks Brian!).
Update September 18, 2009:
If you like the scripts above, you might also be interested in:
- a script to automatically set your working directory in R to your TextWrangler document's directory.
- a start-up script for R with a couple of useful function that should've been in R in the first place.