[TxMt] Re: R send function to console

Hans-Jörg Bibiko bibiko at eva.mpg.de
Wed Mar 5 11:27:57 UTC 2014


Hi Ross,

On 05 Mar 2014, at 11:26, Ross Ahmed <rossahmed at googlemail.com> wrote:

> Let's say I have R open and i’m editing a .R filewith TextMate. In my .R file I have a line of code like this:
> 
> lm(mpg ~ drat, mtcars)
> 
> Is it possible to set up a keyboard shortcut, which when pressed whilst an R object was selected, would send that object to the R console inside a function.
> 

this is rather simple since you can communicate with R via AppleScript.

Create a new tmCommand with:
----------------------------------------
#!/usr/bin/env bash
[[ -f "${TM_SUPPORT_PATH}/lib/bash_init.sh" ]] && . "${TM_SUPPORT_PATH}/lib/bash_init.sh"


Rcmd="length($TM_SELECTED_TEXT)"

osascript <<-APPLESCRIPT
tell application "System Events"
    tell application "R" to cmd "${Rcmd//\"/\"}" 
end tell
APPLESCRIPT
----------------------------------------

Input: Selection; format: Text
Output: Discard
Key Equivalent: "whatever you like"

The 4th line of code is the interesting part:

- $TM_SELECTED_TEXT holds the content of the current selection
- Rcmd is the variable whose content will send to R.app - thus in that example it will send "length({content of selection})"  


Hope it helps.
Cheers,
Hans




More information about the textmate mailing list