SCIENTIFIC SOFTWARE AND SERVICES FOR RESEARCH
IN SYSTEMS BIOLOGY AND MEDICAL CYBERNETICS
Welcome
Software
Code
Services
Docs
Internal
Contact

Code

How to use an external editor for R on Mac OS X:

On the Mac, R's built-in editor is much better than it is in the Windows version of R. However, for the advance in comfort, you have to trade off speed. Especially with large script files and on older machines, the editor tends to be very slow.

The good news is that you can have both, speed and comfort. This is possible with the use of a good external editor, e.g. BBEdit, TextWrangler or SubEthaEdit. Unfortunately, the use of external editors on the Mac platform is not well documented, but with these instructions (that use TextWrangler and SubEthaEdit as example) you should be ready within some minutes.


1. TextWrangler

1.1 Install language support

This step is optional, but recommended. With an appropriate language support file TextWranlger learns to parse the syntax of R. This enables colouring of certain syntactical elements.

In order to complete this step, simply load r.plist and install the file in

~/Library/Application Support/TextWrangler/Language Modules (for the current user, e.g. you)
/Library/Application Support/TextWrangler/Language Modules (for all users)

As usual, ~ denotes the path to your (or the respective user's) "home" folder in Mac OS X.

1.2 Install connection script

This step is required to be able to execute the script in R. Open AppleScript's script editor and enter the following code:

tell the application "TextWrangler"
  set sel to selection of front window as string
  set selLength to length of sel
    if selLength > 0 then
      tell the application "R"
        activate
        cmd sel
     end tell
    else
      set MacPath to file of front window as string
      set UnixPath to POSIX path of MacPath
      tell the application "R"
        activate
        cmd "source(\"" & UnixPath & "\",echo=FALSE)"
      end tell
    end if
end tell

Of course, this script is also available for download.     "Execute in R" script for TextWrangler

The code should be saved as script in one of the following folders:

~/Library/Application Support/TextWrangler/Scripts (for the current user, e.g. you)
/Library/Application Support/TextWrangler/Scripts (for all users)

Again, ~ denotes the path to your (or the respective user's) "home" folder in Mac OS X.

1.3 Finishing

Essentially, you are done now. Optionally, you may adapt the coloring of syntactical elements with TextWrangler's preferences (pane "Languages").


Click to enlarge

Additionally, you may assign a keyboard shortcut for executing the AppleScript (e. g. Command and Return, as in R's editor). You can do this with TextWrangler's script pallette.


Click to enlarge




2. SubEthaEdit

2.1 Install language support

This step is optional, but recommended. With an appropriate language support file (also referred to as "mode") SubEthaEdit learns to parse the syntax of R. This enables colouring of certain syntactical elements.

In order to complete this step, simply load RS-Plus.mode and install the file in

~/Library/Application Support/SubEthaEdit/Language Modules (for the current user, e.g. you)
/Library/Application Support/SubEthaEdit/Language Modules (for all users)

As usual, ~ denotes the path to your (or the respective user's) "home" folder in Mac OS X.

2.2 Install connection script

This step is required to be able to execute the script in R. Open AppleScript's script editor and enter the following code:

on seescriptsettings()
  return {displayName:"Execute in R", shortDisplayName:
  "Run in R", keyboardShortcut:"^@r", 
  toolbarIcon:"ToolbarIconRun", inDefaultToolbar:"yes", 
  toolbarTooltip:"Execute script or S source code fragment in R", 
  inContextMenu:"yes"}
end seescriptsettings
												
tell the application "SubEthaEdit"
  set sel to contents of selection of front window as string
  set selLength to length of sel
  if selLength > 0 then
    tell the application "R"
      activate
      cmd sel
    end tell
  else
    set MacPath to path of front document
    set UnixPath to POSIX path of MacPath
    tell the application "R"
      activate
     cmd "source(\"" & UnixPath & "\",echo=FALSE)"
    end tell
  end if
end tell
												
											
Of course, this script is also available for download.     "Execute in R" script for SubEthaEdit

The code should be saved as script in one of the following folders:

~/Library/Application Support/SubEthaEdit/Scripts (for the current user, e.g. you)
/Library/Application Support/SubEthaEdit/Scripts (for all users)

Again, ~ denotes the path to your (or the respective user's) "home" folder in Mac OS X.

2.3 Finishing

Essentially, you are done now. Optionally, you may adapt the coloring of syntactical elements with SubEthaEdit's preferences (pane "Styles", then select "R/S-Plus" in the popup menu). A shortcut for the script may be determined by a seescriptsettings()-Handler in the script. See the appropriate documentation page of TheCodingMonkeys for details.

3 Conclusions

These hints should help you to speed up R, especially on older Macs, while retaining (and perhaps even improving) the comfort of R's editing capabilities.

 

 

 

© 1999 - 2016 J. W. Dietrich
Imprint and legal information | Last revision: Wed, Feb 24, 2016