Home Download Buy Blog Forum Support

using Sublime Text 2 as an editor for R in Windows

using Sublime Text 2 as an editor for R in Windows

Postby hootener on Mon Mar 05, 2012 3:46 pm

This question has probably been asked before, but it's a difficult one to search for (at least I'm having trouble with it), since "R" is a pretty broad search term and "R stats/stastistics/etc." doesn't seem to bring up anything.

In any case, I'm really digging Sublime Text 2 and would love to see/make/use a system for R similar to the LaTeXTools package, where you can type your code in ST2, hit ctrl+R and have it executed in R, either in the RGUI or the command line, to me it doesn't matter which.

Surely this has been done already (how can it not be? I mean, there's even syntax coloring for R in ST2) and I just can't find the appropriate forum posts. If it hasn't, I don't mind working on it myself, but any help (e.g., pointing me to relevant forum posts) would be appreciated.

Thanks

EDIT: I achieved a pretty viable solution through fiddling around with SublimeREPL. Thanks.
hootener
 
Posts: 7
Joined: Mon Mar 05, 2012 3:40 pm

Re: using Sublime Text 2 as an editor for R in Windows

Postby wuub on Mon Mar 05, 2012 5:39 pm

hootener wrote:EDIT: I achieved a pretty viable solution through fiddling around with SublimeREPL. Thanks.


Glad to hear it :) Don't hesitate to post bugs or even minor inconveniences here https://github.com/wuub/SublimeREPL/issues
I've got very little feedback regarding R support in SublimeREPL and would really like to know what is missing.
SublimeREPL via Package Control & SublimePTY dev/pre-alpha. Donations are appreciated, but not required :) Twitter: @wuub
wuub
 
Posts: 218
Joined: Wed Aug 10, 2011 8:16 am
Location: Poland

Re: using Sublime Text 2 as an editor for R in Windows

Postby hootener on Mon Mar 05, 2012 6:18 pm

Actually there were some issues I had to sort out. I will post them here for anyone that wants to replicate what I did, because I'm not sure if it's an issue with my configuration or SublimeREPL in general.

The main problem I had was that SublimeREPL couldn't find R, even when placing R's directory into my PATH. Thinking that maybe SUblimeREPL didn't have access to my User PATH variable, I also tried out adding R to the System PATH, to no avail. I only got things working when I hardcoded the path to R in the Main.sublime-menu file in AppData/Roaming/Sublime Text 2/Packages/SublimeREPL/config/R. Also, I double checked to make sure I could call R from the command line by just typing "R" at the cmd.exe prompt, which worked and verified that R was placed in my path appropriately. So, while it's a strange issue, at least a workaround exists.

An idea for a feature improvement would be to add a REPL setting for RScript. Currently, I use R to launch the interpreter in Sublime if I want to copy paste code into it from an opened R file. But have a REPL for RScript allows me to select RScript from the SublimeREPL drop downi n the Tools menu and (if the R file I'm interested in is active) it automatically gets passed to and ran by RScript, generating output in a REPL tab in Sublime Text 2. It's pretty nice. Honestly, this probably isn't a necessary feature to implement since it is pretty easy to figure out on one's own, but I thought I'd mention it nonetheless.

As an interesting bonus, all plot functions in R seem to output to .pdf files that get stored in the working directory rather than opening in new windows. This is a benefit to me since, more often than not, if I'm plotting I need to .pdf for a publication or something, but I'm mentioning it here in case anyone is curious as to why their plots aren't showing up when calling plot from an R REPL.

Thanks for the tool, wuub. SublimeREPL is definitely pretty slick, and a big help to my workflow.
hootener
 
Posts: 7
Joined: Mon Mar 05, 2012 3:40 pm

Re: using Sublime Text 2 as an editor for R in Windows

Postby wuub on Mon Mar 05, 2012 6:49 pm

I need to man up and finally finish a real documentation for SublimeREPL :/

hootener wrote:The main problem I had was that SublimeREPL couldn't find R, even when placing R's directory into my PATH.

Problems with PATH are actually the most common ones. Best way to fix them is to place SublimeREPL.sublime-settings file in your Packages/Default directory and fill default_extend_env as required:
Code: Select all
{
   "default_extend_env": {"PATH": "{PATH};c:\\something\\something\\dark\\side"}
}


Thinking that maybe SUblimeREPL didn't have access to my User PATH variable, I also tried out adding R to the System PATH, to no avail.

Sorry for asking such obvious question, but did you restart SublimeText after PATH change?

An idea for a feature improvement would be to add a REPL setting for RScript.

Will look into it.

copy paste code into it from an opened R file

Try F2,l (eval lines w/ cursor in them), F2,s (eval selection) and F2,f (eval file)

in case anyone is curious as to why their plots aren't showing up when calling plot from an R REPL.

Hm. Now I'm curious...
SublimeREPL via Package Control & SublimePTY dev/pre-alpha. Donations are appreciated, but not required :) Twitter: @wuub
wuub
 
Posts: 218
Joined: Wed Aug 10, 2011 8:16 am
Location: Poland

Re: using Sublime Text 2 as an editor for R in Windows

Postby hootener on Mon Mar 05, 2012 8:30 pm

I need to man up and finally finish a real documentation for SublimeREPL :/


Please do. This is a pretty awesome tool. The more I tinker with it the more robust I realize it is.

Sorry for asking such obvious question, but did you restart SublimeText after PATH change?

Indeed I did, to no effect. I will probably incorporate your previous suggestion involving SublimeREPL.sublime-settings, though.

Try F2,l (eval lines w/ cursor in them), F2,s (eval selection) and F2,f (eval file)


These techniques pretty much obviate the need for the RScript setup I mentioned in the previous post. I didn't realize they existed or I would've used them from the outset. Perhaps I just missed them in the documentation or something. The benefit (or downside) to these approaches versus running RScript is that the REPL persists after the R file is ran, meaning you can continue to query variables and such from the R REPL tab. Dedicating a REPL setup to RScript only gains you the benefit of terminating the RScript process when the script finishes. Good for batch work, perhaps?

Hm. Now I'm curious...

If you find some method to allow plots to be viewed in separate windows, please let me know. Like I said, the pdf approach usually works fine for me, but it would be nice to have window support as well. I may look into it myself if I get some free time.

EDIT: A bug(?) I forgot to mention...if R is open in a REPL tab and you make an error (e.g., attemping to print the contents of a variable that doesn't exist), the appropriate R error message will print, but the REPL will halt execution, meaning you have to bring up a new REPL. It would be greatly preferred if the REPL tab continued to persist after errors, since they can happen pretty frequently in R, and the halt may cause a loss of data/work if a user is working with the REPL directly and not through an R script.
hootener
 
Posts: 7
Joined: Mon Mar 05, 2012 3:40 pm

Re: using Sublime Text 2 as an editor for R in Windows

Postby wuub on Mon Mar 05, 2012 8:49 pm

On OSX everything works correctly (referencing unknown variable prints an error and REPL continues to work) also plots are shown in a separate window. I expect on linux it's the same story.
It's time to wrestle with Windows... again... ;)

EDIT5:
disregard those and see next post :)

EDIT: also, default_extend_env seems to have little effect on Windows, it's correctly applied (i,e. PATH is modified) but makes no difference wrt to subprocess being able to run R.exe.Stay with your hardcoded solution for now.

EDIT2:
Closing R.exe after an error can be changed with:
Code: Select all
options(error=utils::recover)

Put it at the end of ./R/R-2.14.2/etc/Rprofile.site it should be work.

EDIT3:
And our PATH problems on Windows are explained: http://bugs.python.org/issue8557
It's possible to easily "fix" that with a simple shell=(os.name == 'nt'), in subporcess.Popen() but I need to think it through, unfortunately running a process under shell=True makes it very difficult to kill the child on tab close.

EDIT4:
R thinks it is not running interactively on Windows, That's why plots are saved and EDIT2 was required. I'm investigating what can be done about it.
Code: Select all
> interactive()
[1] FALSE
Last edited by wuub on Mon Mar 05, 2012 9:46 pm, edited 1 time in total.
SublimeREPL via Package Control & SublimePTY dev/pre-alpha. Donations are appreciated, but not required :) Twitter: @wuub
wuub
 
Posts: 218
Joined: Wed Aug 10, 2011 8:16 am
Location: Poland

Re: using Sublime Text 2 as an editor for R in Windows

Postby wuub on Mon Mar 05, 2012 9:46 pm

Ok. For now update config/R/Main.sublime-menu to this. I'll review this and shell=true tomorrow and if no regression is found it will make to SublimeREPL v1.0.20
Code: Select all
[
     {
        "id": "tools",
        "children":
        [{
            "caption": "SublimeREPL",
            "mnemonic": "r",
            "id": "SublimeREPL",
            "children":
            [
                {"command": "repl_open",
                 "caption": "R",
                 "id": "repl_r",
                 "mnemonic": "r",
                 "args": {
                    "type": "subprocess",
                    "external_id": "r",
                    "encoding": "utf8",
                    "cmd": {"linux": ["R", "--interactive", "--no-readline"],
                            "osx": ["R", "--interactive", "--no-readline"],
                            "windows": ["C:\\Program Files\\R\\R-2.14.2\\bin\\i386\\Rterm.exe", "--ess"]},
                    "cwd": "$file_path",
                    "extend_env": {"osx": {"PATH": "{PATH}:/usr/local/bin"},
                                   "linux": {"PATH": "{PATH}:/usr/local/bin"},
                                   "windows": {}},
                    "cmd_postfix": "\n", // postfix that will be automatically added after a line in repl
                    "suppress_echo": {"osx": true,
                                      "linux": true,
                                      "windows": false},
                    "syntax": "Packages/R/R.tmLanguage"
                    }
                }
            ]
        }]
    }
]
SublimeREPL via Package Control & SublimePTY dev/pre-alpha. Donations are appreciated, but not required :) Twitter: @wuub
wuub
 
Posts: 218
Joined: Wed Aug 10, 2011 8:16 am
Location: Poland

Re: using Sublime Text 2 as an editor for R in Windows

Postby hootener on Mon Mar 05, 2012 10:04 pm

EDIT 2 appears to fix the R.exe terminating after an error. Great!

Your updated Main.sublime-menu fixes the plot issue as well. I'm now plotting in windows. Thanks!

Awesome turnaroud on this, wuub. Good work!
hootener
 
Posts: 7
Joined: Mon Mar 05, 2012 3:40 pm

Re: using Sublime Text 2 as an editor for R in Windows

Postby hootener on Tue Mar 20, 2012 4:11 pm

I thought I would post an update concerning using ST2 with R in Windows since I have been using the setup exclusively for a couple of weeks now. There are some pros, some cons, and a bug I thought I'd point out. wuub, if you want the bug(s) posted to the Github repo, let me know and I can do that.

Pros:
- Syntax highlighting (both in .R files and in the REPL window for R). I never realized how awesomely helpful a syntax highlighted R-terminal window would be. It's great.
- Text-file like navigation of the R REPL window. It's helpful (sometimes) to be able to use the arrow keys to navigate through the terminal and terminal output history like it's one big text file. However, this does have some drawbacks
- Numerous other ST2-related benefits. auto-complete, multiple cursors, etc.
- Plotting to multiple windows (vs overwriting the content of a single window) by default
- Launching the R REPL using the current directory as the working directory

Cons (vs. the classic R GUI):
- No tab-complete in the REPL. Coming from the RGUI, a handy feature for member access is to do something like the following: object$, followed by the tab key which allows for tabbing through the members of the object. This is not unlike tab-complete in any other shell. In an R REPL, however, since the terminal is manipulated like a text file, the tab key works as tab would in a text editor. The same is true for the enter key. So, to run a line, for example, I have to have ensure that the cursor is at the end of the line and then hit Enter. This con is by no means a deal-breaker for me, and in many cases the REPL behaving like a text document is beneficial. It's just something I thought I'd point out if anyone else considers going with the R REPL setup. In short, the REPL acts like a text file, and is therefore not manipulated in the way you intuitively expect command line interpreters to work. Not a bad thing, just takes some getting used to, and some may consider it a con.

Bugs:
- Closing an R REPL tab in ST2 sometimes does not close the RTerm.exe process, leading to running processes that must be killed via Ctrl+alt+del. This isn't a huge hindrance in most cases; however, sometimes the orphaned Rterm.exe process will begin to consume massive amounts of CPU. On my Core2Duo machine for example, the process will consume 50% CPU, dominating a single core. This leads to massive slowdown and, to someone unfamiliar with the issue, can cause quite a bit of frustration since the computer essentially bogs down with no obvious reason. I have verified that this bug exists on multiple machines (all Win 7). Other members of my lab (who have seen firsthand how much better the SublimeREPL approach to R is over the traditional R GUI), have also noticed the issue on their setups, again all Windows 7 machines. I have yet to test if a similar problem arises on OS X, but I can if necessary. Also note that the Rterm.exe process can always be killed effectively (in Windows) using Ctrl+Alt+Del. In Unix-y terms, the process isn't a zombie, just an orphan.

This may just be a general SublimeREPL bug that I'm not familiar with, but I thought I'd point it out in any case.

TL;DR: R + SublimeREPL = happiness, Cons exist but they're minimal when compared to the pros, an orphan process bug within SublimeREPL (when using R) seems to exist.

Thanks!
hootener
 
Posts: 7
Joined: Mon Mar 05, 2012 3:40 pm

Re: using Sublime Text 2 as an editor for R in Windows

Postby wuub on Tue Mar 20, 2012 5:41 pm

>> since I have been using the setup exclusively for a couple of weeks now.

Thank you very much. It's nice to know that someone actually finds this useful.

>> wuub, if you want the bug(s) posted to the Github repo, let me know and I can do that.

Nah. I sometimes report bugs myself to close them with a commit message for future reference. But this orphaned process problem is already known (https://github.com/wuub/SublimeREPL/issues/17)

>> No tab-complete in the REPL (...) REPL acts like a text file, and is therefore not manipulated in the way you intuitively expect (emphasis mine)

<brain-dump>
There are two extreme approaches to embedding a generic interpreter inside a text editor. One is through standard text editing mode somehow piped to/from the interpreter (SublimeREPL) other is terminal emulation (SublimePTY). Problem is, I'm not happy with either of those. SublimeREPL looses all the terminal/console functions, while PTY looses all the nice editing properties of the editor.
There is a much better (in terms of usability) middle ground, something I like to call "cooperative interpreter". Basically a standard REPL, but one that knows it's being run inside ST2 and provides a side channel communication for autocomplete, thread management (think runaway loop) and maybe some better text-transfer/debugging facilities (+- what most interpreter GUIs do but with better front-end/back-end separation) . Sounds tempting, but there is a catch. There is no generic way to do it, you need a separate cooperative interpreter for each language you wish to support this way. I would probably manage to code something for python in my spare time, but that's it, with my recent workload everything is progressing superslow either way.
</brain-dump>

To cut the long story short: I know this is an issue, I bump into it every day, I think I know how to fix this, the only problem is lack of resources :|

>> Closing an R REPL tab in ST2 sometimes does not close the RTerm.exe process,

There is an ongoing issue with killing some types of subprocess REPLs (mostly on Windows). For some (e.g. Clojure, Scala) I used an easy to implement "soft_quit" solution that sends a "graceful exit" string before trying to kill the process. I've got reports that it helps (a bit).
Next version of SublimeREPL will include a soft_quit config for R:
Code: Select all
"soft_quit": "\nquit(save=\"no\")\n",

It may help if RTerm is still responding but decided to ignore SIGTERM for some reason.

>> This may just be a general SublimeREPL bug that I'm not familiar with, but I thought I'd point it out in any case.

a) this is a known problem, b) I was unable to recreate it with RTerm. Can you think of a use case leading to an orphaned RTerm?
SublimeREPL via Package Control & SublimePTY dev/pre-alpha. Donations are appreciated, but not required :) Twitter: @wuub
wuub
 
Posts: 218
Joined: Wed Aug 10, 2011 8:16 am
Location: Poland

Next

Return to Technical Support

Who is online

Users browsing this forum: Google [Bot] and 33 guests