Using TextMate with R

rlogo

I’ve been using the R language for some years now but didn’t have it installed on my current machine.

TextMate is a great editor to use as it has a custom bundle that gives you syntax highlighting and executes commands for you in the R console.

To install the R bundle:

cd ~/Library/Application\ Support/TextMate/Bundles/
svn co http://svn.textmate.org/trunk/Bundles/R.tmbundle/

Then in TextMate choose Bundles->Bundle Editor->Reload Bundles.

There is one tweak I make which is that I like to press ⌘ + RETURN to execute the current selection in the R console. So edit the bundle and find R.app in the commands and change the key. Also I like it to return back to text mate afterwards, so I add the following to the script:

-e ‘tell application “TextMate” to activate’ \

I also removed the line about setting the current dir. This was a little fiddly – I found that I had to make sure I selected the entire line and cut it then delete, making sure there are no end of line characters. So it looks like this:

# input is selection or document
rawText=”`cat`”
curDir=”
if [[ ${#TM_DIRECTORY} -gt 0 ]]; then
	curDir=”$TM_DIRECTORY”
fi
osascript -e ‘on run(theCode)’ \
		  -e ‘tell application “R” to activate’ \
		  -e ‘tell application “R” to cmd (item 1 of theCode)’ \
		  -e ‘tell application “TextMate” to activate’ \
		  -e ‘end run’ — “$rawText” “$curDir”

All good.

UPDATE: 07/02/2011 – R now comes with a 64bit version so if you are on a macbook you can replace “R” in the code above with “R64″

Share

2 thoughts on “Using TextMate with R

  1. Hey there,

    you sound like you know a thing or two about Textmate-Bundles, so I have a short question: I’m trying to run JGR (Java Gui for R) instead of R.app.

    I tried simply exchanging R with JGR in the script above, but the only thing I get is: “119:120: syntax error: Expected end of line, etc. but found “””. (-2741)”.

    Do you have some idea how to do this?

  2. Hi, sorry I’ve not responded in a timely fashion! I meant to investigate but have unfortunately been swept away by work! Hope you found a solution and again many apologies.

Comments are closed.