Wörterbuch editor

Compiling R Code in Sublime Text

Lukas Strömsdörfer Blog, Data Science, Statistik

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 snytax 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 ususally is to change the theme. Soon the intervalls 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 interprete 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 setup your Sublime Text 3 to interprete 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 who can help acheive 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 stabilty. 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.

Über den Autor
Lukas Strömsdörfer

Lukas Strömsdörfer

I am a data scientists at STATWORX, apart from automating my job, I am taking my vintage bike for a spin and building a ML tool that lets me become a below-average gardener.