TextWrangler and R

(This article was first published on macsci, and kindly contributed to R-bloggers)

Rlogo
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/, 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!).
    textWranglerIcon

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.

To leave a comment for the author, please follow the link and comment on his blog: macsci.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags:

Comments are closed.