en
                    array(1) {
  ["en"]=>
  array(13) {
    ["code"]=>
    string(2) "en"
    ["id"]=>
    string(1) "1"
    ["native_name"]=>
    string(7) "English"
    ["major"]=>
    string(1) "1"
    ["active"]=>
    string(1) "1"
    ["default_locale"]=>
    string(5) "en_US"
    ["encode_url"]=>
    string(1) "0"
    ["tag"]=>
    string(2) "en"
    ["missing"]=>
    int(0)
    ["translated_name"]=>
    string(7) "English"
    ["url"]=>
    string(78) "https://www.statworx.com/en/content-hub/blog/compiling-r-code-in-sublime-text/"
    ["country_flag_url"]=>
    string(87) "https://www.statworx.com/wp-content/plugins/sitepress-multilingual-cms/res/flags/en.png"
    ["language_code"]=>
    string(2) "en"
  }
}
                    
Contact
Content Hub
Blog Post

Compiling R Code in Sublime Text

  • Expert Lukas Strömsdörfer
  • Date 08. January 2018
  • Topic CodingRTutorial
  • Format Blog
  • Category Technology
Compiling R Code in Sublime Text

What is Sublime Text?

Nearly every coder has at one point googled for the best code editor. To those who did, you already know Sublime Text. To those who didn’t: best code editors. After its initial release in 2007 Sublime Text has for sure made its way into the ranks of the most popular editors. Here at STATWORX, most of us are using Sublime Text on a daily basis. The editor – like many others of course – supports syntax highlighting for a variety of programming languages. Among others, our main tools like Python, R, and its extensions.

Sublime itself is not free of charge, however, you can download a free trial on their website. Since Version 2.0 macOS and Linux are supported besides Windows.

Sublime as an alternative to RStudio?

The editor of choice for most R users is RStudio. The graphical interface of the IDE is highly developed and provides a variety of convenience functions such as Git integration, a console, a terminal, a plot output window, and many others. The latest update even allows the user to choose dark themes. I am quite fond of RStudio myself, however, working with the same interface day-in-day-out does get a little tiring. Furthermore, RStudio’s interface is quite packed, sure with useful features. Though, while coding you probably do not need most of those and might prefer a more minimalistic editor. I am sure most of you know what I mean. The first step usually is to change the theme. Soon the intervals in which you keep your theme are getting shorter with every change. If you feel that way right now, or will at some point, I will tell you what I did to overcome this tragic spiral of theme changes.

Setting up Sublime to interpret R Code

After a period of intense RStudio theme changes, I decided to switch to Sublime Text 3 for R coding. Per default, Sublime provides syntax highlighting for normal R code, R documentation, and R markdown. However, the code cannot be executed. With a few simple tricks though, you can set up your Sublime to send R code to an active R Session in the terminal.

The first thing to do is to install the corresponding Plug-ins. To install Plug-ins in Sublime you simply go to the Command Palette.

sublime palette

The command palette essentially is a search bar that lets you navigate through the interface and the corresponding libraries. Typing in “Package Control: Install Packages”, will allow you to look for any sort of Plug-in or package you want to install.

sublime install

Select “Package Control: Install Package” and you will be able to connect to repositories to install Plug-ins. You can now check for your desired Plug-ins and simply download them.

sublime packages

There are several ways in which you can set up your Sublime Text 3 to interpret R code. I prefer a setup in which I write my code in a Sublime script and send it to an active R session in my terminal.

sublime R Code

You can recreate a similar setup using two Plug-ins: (i) SendCode and (ii) R-Box. There are several Plug-ins that can help achieve similar goals, of course. For some reason, I appreciate this one. In case you are not so fond of this setup you might want to check out SublimeREPL, SendText, and R-Snippets.

Assuming you are happy with my setup, you can use the command palette to install these two packages. Once you have done that, you can actually get to it right away. Simply follow these steps:

  1. open a new file and save it .R
  2. open an R session in your terminal
  3. write your R code like it was a script in RStudio
  4. send it to the terminal hitting CMD + ENTER (on a Mac at least)

That is pretty much everything you have to do. By the way, the theme I am using is from the Materialize Plug-in; credits to the authors, it’s amazing. I am currently using the One Dark theme – for today anyway.

Resume

I have been using this setup for a couple of weeks now. So far, I can say that I am impressed with the stability. Everyone working with RStudio is acquainted with the R Session aborted message and the nasty and somewhat sadistic-looking bomb that comes with it. As my setup sends code directly to an R Session running in the terminal, it is far more stable. Furthermore, I enjoy the plain design of Sublime’s interface and the degree to which I can customize the editor. A crucial feature, however, is missing. R is – at least to some extent – object-oriented. RStudio, therefore, provides an integrated workspace browser, listing all objects in the workspace, as well as their type. Especially when working with a lot of objects, it does get a bit annoying to manually browse the workspace.

Overall, I can say that I very much enjoy the change of pace and the stability for sure is a major benefit. Eventually, though you might hear from me again with a piece on how to set up another code editor because I grew tired of my current setup. Lukas Strömsdörfer Lukas Strömsdörfer

Learn more!

As one of the leading companies in the field of data science, machine learning, and AI, we guide you towards a data-driven future. Learn more about statworx and our motivation.
About us