|
||||||||||||||||||||||||
|
Code How to use an external editor for R on macOS / 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 BBEdit, TextWrangler and SubEthaEdit as
example) you should be ready within some minutes. 1. BBEdit 1.1 Install language support This step is optional, but recommended. With an appropriate language support file BBEDit learns to parse the syntax of R. This enables colouring of certain syntactical elements. In order to complete this step, simply load Marc Bearak's r.plist and install the file in ~/Library/Application
Support/BBEdit/Language Modules (for the current user, e.g. you) As usual, ~ denotes the path to your (or the respective user's) "home" folder in macOS. 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 "BBEdit" 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
The code should be saved as script in one of the following folders: ~/Library/Application Support/BBEdit/Scripts (for
the current user, e.g. you) Again, ~ denotes the path to your (or the respective user's) "home" folder in macOS / OS X. 1.3 Finishing Essentially, you are done now. Optionally, you may adapt extension mappings and other language-specific settings with BBEdit's preferences (panes "Languages" and "Text Colors"). Additionally, you may want to assign a keyboard shortcut for executing the AppleScript (e. g. Command and Return, as in R's editor). You can do this with BBEdit's script pallette. 2. TextWrangler 2.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) 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: 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
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) 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 TextWrangler's preferences (pane "Languages"). 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.
3. SubEthaEdit 3.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) As usual, ~ denotes the path to your (or the respective user's) "home" folder in Mac OS X. 3.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
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) Again, ~ denotes the path to your (or the respective user's) "home" folder in Mac OS X. 3.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. 4 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. |
|||||||||||||||||||||||
|
|
|||||||||||||||||||||||
|