Readying SublimeText

PackageControl

This is the package of packages for Sublime, allowing you to install new packages right from Sublime, with the nifty cmd + Shift + P shortcut. If you have SublimeText 3, just press ctrl+`, then copy and paste the following in:

import urllib.request,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa1548d1514676163dafc88'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)

Complete details, installation guides for ST2, and advice on troubleshooting is here.

Basic R functionality

R-Box

Now that we’ve added PackageControl, let’s make Sublime actually understand R and help us like an IDE (think R Studio) would, offering autoComplete suggestions and more.

To do this, type cmd+shift+P, then type install, at which point Sublime will suggest Package Control: Install Package as an option. Navigate to it, hit enter and in the new text box, type R-Box. This should download the R-Box package for ST, some features of which I have listed below.

  • Autocompletions for various packages.
  • Function hints in status bar for various packages.
  • Extended R Syntax
  • Support for Roxygen, Rcpp, R Sweave and R Markdown syntaxes.
  • Build commands for Rmarkdown and Rnw files.

More Autocomplete

NOTE: On second thought, this subsection is possibly redundant after you’ve installed R-Box, so feel free to skip it.

For even more extensive autocomplete support, you can give Sublime a list of word-portions you’d like it to offer autocomplete for. To integrate this list into Sublime, we manually add this list to our key-binding preferences.

To do this, on the menu bar, navigate to Sublime Text > Preferences > Key Bindings-User, and copy and paste the comprehensive list from my Google Drive (Essentially, just open the .json file with a text editor, select all, then copy and paste it onto the user key bindings file.)

Coding in R

To help with the actual coding portion, I recommend downloading the Terminal and SendText packages, just like you installed the R-Box package, using Package Control.

Terminal

When downloaded, pressing cmd+Shift+T from any file open in Sublime opens a terminal in the folder containing the currently edited file (or a terminal in the project folder containing the currently edited file, if you so configure it.)

SendText

This package sends selected text to an open terminal window to be run. Once you select the portion of your code you’d like to execute, simply hit cmd+Enter and the package will run the snippet in the Terminal.

When used in combination, these two packages can save you a ton of time by getting right to the code.

Some customization

Sublime Text offers built in functionality for snippets – which are kind of like text expanders (the same things that on your phone keyboard turn omw into “On my way!”).

Snippet installation

To create a new snippet (or to copy the ones below for your use), from the menu bar, just navigate to Tools > Developer > New Snippet, manipulate the template as per your needs and save it as snippetName.sublime-snippet. Be sure to change the extension to .sublime-snippet, or you run the risk of Sublime refusing to acknowledge the existence of the snippet you so (presumably) meticulously created (or copied).

You can add snippets for any code portion that you write often. For instance, the following is a snippet I wrote for the header.

<snippet>
    <content><![CDATA[
##############################################################################

# Project:     ${1:project}
# Author:      ${2:Shreyas Lakhtakia}
# Date:        ${3:2016}
# Title:       ${4:title}
# Description: ${5:A brief description}

##############################################################################

${6:Begin code here}

]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>rheader</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <scope>source.r</scope>
</snippet>

With this snippet in place, all I need to do is type rheader and press tab and Sublime will expand it into the giant header template you see above. Even better, you don’t have to use your mouse to select the text fields you want to edit. Just hit tab and change away sequentially!

More Snippets

Here are some more useful snippets that I created.

For sections in code -
<snippet>
    <content><![CDATA[
# ============================================================================

# Section:     ${1:section_title}
# Description: ${2:A brief description}

# ============================================================================

]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>rsection</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <scope>source.r</scope>
</snippet>
For subsections in code -
<snippet>
    <content><![CDATA[
# 
# ${1:subsection_title}
# ---------------------------------------------------------------------

${2:more_code}

]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>rsub</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <scope>source.r</scope>
</snippet>

Bells and whistles

Theming

Install themes to make Sublime look awesome. Themes are installed in the same way packages are, using PackageControl. Here’s a list of cool themes to get you started -

  • Boxy
  • Material
  • Agila

Once installed, on the menu bar, navigate to SublimeText > Preferences > Color Scheme and then take your pick!

You’ve made it this far into the guide? How bored are you? Go watch the Olympics instead. Or the news, really - it’s been just as interesting off late.