HiveBrain v1.2.0
Get Started
← Back to all entries
patternbashMinor

Sublime Text 2 R Build System (source file, send selection to interactive session)

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sublimefileselectioninteractivesourcetextsystemsessionsendbuild

Problem

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  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 xdot

Solution

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.

Code Snippets

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
# in this workaround
# I could not find a better way to properly 
# catch the terminator ending,
# after it receives the 'Running R' name.

Context

StackExchange Code Review Q#78181, answer score: 3

Revisions (0)

No revisions yet.