4
\$\begingroup\$

The following is the description of the R build system I am using (very happy) with Sublime Text 2 on linux (Crunchbang). With it, I am able to:

  • to source the current file.R to an interactive R session;
  • to send the current selection to an interactive R session (supports multi selection);
  • to output plots and *.Rout files (non-interactive);

xsel, xdotool, terminator (or some other terminal that supports custom titles) and a bash script (see below) are requisites. Please, attention for the locations of each file.

The open_and_run_r.sh script is the object of revision (see its commented lines for details). But feel free to give your considerations about the whole picture.


Build File:

.config/sublime-text-2/Packages/User/r.sublime-build

{ // source( $file ) into interactive session
 "shell": true,
 "cmd": 
 [ "echo 'source(\"'$file'\")' | xsel -i -b; if xdotool search --name 'Running R' windowactivate --sync; then xdotool key --window 0 ctrl+shift+v; else open_and_run_r.sh; fi; xsel -c -b"
 ],
 "selector": "source.r",
 "variants":
 [
 { // send current selection of $file to interactive session; supports ST2 multi-selection as well :)
 "name": "r_send_selection",
 "shell": true,
 "cmd":
 [ "xdotool getactivewindow key ctrl+c; if xdotool search --name \"Running R\" windowactivate --sync; then xdotool key --window 0 ctrl+shift+v Return; fi"
 ]
 },
 { // generate custom output.Rout file (not the output.Rout from R CMD BATCH), non-interactive
 "name": "r_output",
 "cmd":
 [ "/usr/bin/R --quiet --slave < $file > output.Rout"
 ]
 }
 ]
}

Lets give a proper formatting to the bash commands:

# source file, note:
 # escaped \" for JSON compatibility;
 # $file is ST2 global variable;
 # 'source()' is an R command 
echo 'source(\"'$file'\")' | xsel -i -b;
if xdotool search --name 'Running R' windowactivate --sync;
 then xdotool key --window 0 ctrl+shift+v;
else open_and_run_r.sh;
fi;
xsel -c -b
# send selection
xdotool getactivewindow key ctrl+c;
if xdotool search --name \"Running R\" windowactivate --sync;
 then xdotool key --window 0 ctrl+shift+v Return;
fi

Finally the open_and_run_r.sh script (run as program enabled).

~/bin/open_and_run_r.sh

#!/bin/sh
echo 'Initializing Terminator...'
terminator --title "Running R" --command='R' &
# in this workaround
# I could not find a better way to properly 
# catch the terminator ending,
# after it receives the 'Running R' name.
a=0
while [ $a = 0 ]; do
 if xdotool search --name 'Running R' windowactivate --sync;
 then a=1;
 xdotool key --window 0 ctrl+shift+v;
 echo 'Initialized.' 
 fi;
 sleep 2; # decreasing the value increases asynchrony behavior.
done 

Keymap file, for keyboard shortcuts.

.config/sublime-text-2/Packages/User/Default (Linux).sublime-keymap

[
// R Build / [ctrl+b] see r.sublime-build/source current $file into interactive R
// R build / send current selection to interactive R
 {"keys": ["super+'"], "command": "build", "args": {"variant": "r_send_selection"} },
// R build / generate output.Rout and plots files from, non-interactive
 {"keys": ["ctrl+shift+b"], "command": "build", "args": {"variant": "r_output"} }
]
asked Jan 21, 2015 at 2:39
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

In the main loop of the script, you don't need the $a variable. You can use an infinite loop, and break out of it when the if condition becomes true. You also don't need the semicolons at the end of the lines.

The script can be simplified to:

while :; do
 if xdotool search --name 'Running R' windowactivate --sync; then
 xdotool key --window 0 ctrl+shift+v
 echo 'Initialized.' 
 break
 fi
 sleep 2 # decreasing the value increases asynchrony behavior.
done 

As for this comment:

# in this workaround
# I could not find a better way to properly 
# catch the terminator ending,
# after it receives the 'Running R' name.

Unfortunately I don't know a better way either.

answered Jan 28, 2015 at 20:06
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.