Using Sublime Text 2 for R
Posted on. by Tom Schenk Jr
My R interface have been pretty basic in the last few years. I have usually stuck to the R console. Yes, I ' ve tried Emacs with ESS; A staple, but it's so unbearably antiquated, it's always gave up in its significant learning curve. GUI packages–especially rstudio–offer viable alternatives, but I feel the GUI lets me lose focus of the code. I have been envious of TextMate for Mac, but alas, I ' m not a Mac user. Recently, though, I ' ve moved to Sublime Text 2. With some nudging, I has been able to mimic the typical R console environment in the more-powerful Sublime Text program.
Want to skip to the basic instructions without the narrative? Just Read the block quotes for specific instructions.
Sublime Text 2 offers a similar experience as textmate (e.g., autocompleting parenthesis and quotation marks) and Integrat Es well with R and other programming languages. R users aren ' t the primary target and it takes a little kick to get the environment perfected for the R community. This was a little tutorial to give R users a similar experience with Sublime Text without ruining the program ' s versatility . That's, you can have a familiar R experience and easily use Sublime Text with programming languages (e.g., JavaScript, c+ +) and typesetting programs (e.g., LaTeX, Markdown, Multimarkdown).
This setup would let you
Browse R code with syntax highlighting
Use the familiar R console within Sublime Text
Send r code from a script to the R control (equivalent to Ctrl + R)
Organizing the layout so the script and console can viewed simultaneously.
The first and last items is natively supported, but we'll need to make some additional adjustments for the second and thi RD Item.
Initial Setup
I'll presume some user knowledge here and would focus on the Windows user. Setup is very easy. Their website have links to executable autoinstallers, you won ' t need to unzip and manual install nor is is you depending on A third party to graciously compile code on your behalf.
Follow the instructions and install using the default settings.
Take a look around
Before proceeding, take a look at the R interface. Similar to other programs (e.g., notepad++), Sublime Text offers native syntax highlighting for R code. Load one of your codes or try a sample script. If you don't see any highlighting, you can always tell Sublime Text by clicking View > Syntax and choosing r. Otherwise you can look in the lower right-hand corner as was shown in the image.
A Nifty feature is autocompleting parenthesis, and other special characters. Type "write.csv (" and it ' ll auto-insert the right paren ")". Little underlines help you match each of the left parenthesis and the right parenthesis. It also works with ' {', ' [', single, and double quotation marks.
Comments has a lighter tone. So does the ubiquitous "<-". Functions and special characters also have highlighting. If you don't like the color scheme, you can change it under Preferences > color scheme. The default is Monokai, Twilight are a very dark theme while iplastic are a very white theme.
Once you the type a variable or function names, you can easily reference it later for autocompletion. Load the data into a variable called "SampleData" and you are need to type ' sa ' and hit tab to complete the name. If There is multiple possible matches, then you can use arrow keys to choose. The limitation of this, however, was that Sublime Text treats "Sample.data" as both separate names. To counter this, I ' ve started the use of underscores, which is friendlier. Also, Sublime Text is using a simple approach--it simply refers to what you ' ve typed before, it's not remembering VARIABL Es in your memory the same is the GUI interfaces do.
Click on Preferences > Key bindings-user, then copy and paste the following (thanks g-force!)
There is plenty of shortcuts. Select a few lines of code and press Ctrl +/ to toggle comments. Ctrl + L 'll select the entire line based on your cursor. Ctrl + G, type in a number and your ' ll jump to that line of code. Type Ctrl +: and type a variable name, it ' ll find all instances. If you had multiple tabs, type Ctrl + P and you can jump to specific tabs.
At the However, you can ' t execute or "compile" code. It ' s just pretty font, helpful quirks, and shortcuts.
Package Manager
Our next goal are to being able use the R console and do calculations within Sublime Text. We ' re going to use the SUBLIMEREPL package to accomplish. Don ' t worry, this isn ' t a rarefied thing that'll clutter your RAM just for R; You'll probably use the this package for the other things too.
Visit the Sublime Package Control website and follow the instructions to install the Package Manager on your computer.
To access the package Manager type 'Ctrl + Shift + P' to bring up a text box. Without using your mouse, begin to type 'Installpackage ' (without quotation marks). You'll see a error related to Git.exe, and you can ignore it at the moment.
Then type "Sublimerepl" and hits enter when it ' s highlighted.
Sublimerepl essentially allows Sublime Text to access compilers or other programs. You can write in Sublime Text and send it to R for the computing. The results is sent back to Sublime Text for display. For you, it'll operate like the normal R console.
However, we need to tell Sublimerepl where R was located on our computer. We need to add R to the Windows Path.
Find where R is installed. To does this, right click on your R icon–whether It's Start menu, desktop, or taskbar. Under the "Shortcut" tab, note the file path under "Target:". Copy and paste the text document, you'll need this shortly.
Now,
Click on Preferences, Package Settings > Sublimerepl > Settings–user. Use the screenshot or example below and paste in the path (thanks wojciech!).
For example, might paste:
Notice in my example the I am ignoring (disabled) Some packages I have installed. needed to add a comma after the bracket on line 8 and pasted the "default_extend_env" within The curly brackets.
Now there are told where Sublime Text can look to find R.
Open the R Console by clicking Tools > Sublimerepl > R. A window would pop up with the familiar R console header.
further customization:r shortcuts
The R console is fantastic because you can quickly execute code from a script. SUBLIMEREPL does has this capability, but the R user would want to use ctrl+r, or something similar. Additionally, the native shortcut to execute script have some issues because it conflicts with other shortcuts already buil T-in Sublime Text.
Sublime Text calls these shortcuts "KeyBindings". Fortunately, they can be edited. However, you had to be careful since Sublime Text had a lot of shortcuts and it's easy to duplicate them, causin G further issues.
I have a proposed set, should be pretty R user friendly. You can select some code (multiple, partial, or a lines) and run it in R using Ctrl + Shift + R. This was similar to the shortcut in the R console (a previous conflict prevents Ctrl + R without a lot of work).
To enable new keybindings:
Go to Preferences > Key bindings–user and paste the code below. If something if already there, then append the code to the bottom (thanks wojciech!).
Using the R language in sublime Text 2