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.
For example: I select 'mpg', press COMMAND + OPTION + L, and this object would be run in the R console as: length(mpg)
I select mtcars', press COMMAND + OPTION + S, and this object would be run in the R console as: summary(mtcars)
I select 'lm(mpg ~ drat, mtcars)', press COMMAND + OPTION + P, and this object would be run in the R console as: plot(lm(mpg ~ drat, mtcars))
Thanks Ross
Hi Ross,
On 05 Mar 2014, at 11:26, Ross Ahmed rossahmed@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
… oops, sorry, much more easier ;)
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 "R" to cmd "${Rcmd//"/"}" APPLESCRIPT ----------------------------------------
Input: Selection; format: Text Output: Discard Key Equivalent: "whatever you like"
Works! Brilliant.
Ross
On 05/03/2014 11:29, "Hans-Jörg Bibiko" bibiko@eva.mpg.de wrote:
oops, sorry, much more easier ;)
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 "R" to cmd "${Rcmd//"/"}" APPLESCRIPT
Input: Selection; format: Text Output: Discard Key Equivalent: "whatever you like"
textmate mailing list textmate@lists.macromates.com http://lists.macromates.com/listinfo/textmate