A guide to using R in Sublime Text

R Studio is a fantastic IDE (Integrated Development Environment). It is simple and intuitive to use, full of helpful features that the programming environment does not provide. The R Studio developers have baked in many extra tools that make the IDE synonymous with the R language for thousands of users (see my post on how RStudio notebooks are an excellent tool for data analysis). But all those features are not always required and make the IDE heavier on resources. In many instances, users need a lightweight and simpler environment. One of the best alternatives to R Studio, which I have used for a long time, is Visual Studio Code with an extension for using R. Though visually what I was looking for — a clutter-free and simple interface, it is on the heavier side (and there are some concerns in users regarding the integrated telemetry). This is where Sublime Text comes. It is a super-fast and minimal code editor with lots of extensions, and just what I needed.

Pre-requisites

  1. Install R (download and install from CRAN).
  2. Add R to Path variable (how-to for Windows). This is required for you to run R code from Sublime Text. You will have to add the whole path for the bin folder inside the installed R folder (such as C:\Program Files\R\R-4.1.0\bin) to the Path variable.
  3. Install Sublime Text. Though it is a paid program, the free version works just fine and only shows an occasional dialogue about the program being unlicensed.

Steps to setup the IDE

Sublime Text has an extension management system, called ‘Package Control’, which can be used to install extensions as well as themes and colour schemes. Package Control is not installed by default, so we need to first install it. Remember the shortcut Ctrl + Shift + P (or Command + Shift + P on Mac), it is used to open the ‘Command Palette’, from where all the commands available to Sublime Text is run. Open the Command Palette using the shortcut, and search ‘install’. The first option should be ‘Install Package Control’. Select it and hit Enter. The installation should not take more than a minute.

Install package control using command palette
Install Package Control using the Command Palette

After Package Control is installed, we will use it to install the required extensions. Open the Command Palette again by hitting the shortcut and search again for ‘install’. This time around, the option ‘Package Control: Install Package’ should be the first. Select it, hit Enter, and wait for Sublime Text to refresh its repositories.

Install packages using package control
Install packages using Package Control

The Command Palette should automatically open when all repositories are loaded, showing you an alphabetical list of extensions you can install.

Repo list
List of extensions

Search for ‘Terminus’, and install the package. This extension is required to open the command prompt (or terminal/console) inside Sublime Text, where the R code will be run.

install terminus
Installing Terminus package

After Terminus is installed, open the Command Palette again, search for ‘install’ and hit Enter to open the list of repositories. Now, search for ‘SendCode’, and install it. This extension is required to send code from the text editor to run in Terminus.

install sendcode
Installing SendCode package

After SendCode is installed, open the Command Palette and search ‘SendCode’. Select the option that says ‘SendCode: Choose Program’ and hit Enter.

sendcode choose program
SendCode: Choose Program

This will open a list of all terminal emulators (consoles/command prompt) to which SendCode can send code. Choose ‘Terminus’ from the list and hit Enter. This will ensure that SendCode registers Terminus to be the default terminal, and sends code to it.

Now, write some R code in the default file which is open, and save the file with the extension .R. Unlike R Studio, you need to save the working file first before running the code. This is necessary in order for Sublime Text to understand which programming language you are using.

Create R file
Create a .R file with some code

Now, time for us to run the code and test that everything is in order. First, open the Command Palette, and search ‘Terminus’. Look for the option ‘Terminus: Open Default Shell in Panel’, select it and hit Enter.

opening terminus
Opening a Terminus window

This will open a Terminus window at the bottom.

new terminus window
New Terminus window at the bottom

Now, type ‘R’ at the prompt, and hit Enter. This will run an instance of R inside Terminus, which you will use to run the code.

running R inside Terminus
Running R inside Terminus

Now, for the final step, select the lines of code, and press Ctrl + Enter. This will run the selection in the R prompt.

If everything works fine, you should see the output in the Terminus window, and we are done! Happy coding! To close the Terminus window, press Ctrl + Shift + W.

Customizations (optional)

There is a little bit of customization I do to suit my workflow. I like the code and output to be side-by-side, not top-and-bottom. To put the Terminus windows to the right side, follow these steps:

Open View > Layout and click on ‘Columns:2’ (or just press Alt + Shift + 2). This will create a new column to the right side.

Dividing the window into two columns

Now, while the cursor is in the right column, open Command Palette, search for ‘Terminus’, and select the option ‘Terminus: Open Default Shell in Tab (View)’.

Opening Terminus in right column

This will open the Terminus window in the right column, instead of the bottom.

Terminus in the right column

Now you can open R in this right pane, and see the code and output side by side.

Code and Output side-by-side

Cheers!

Declaration: I have shown the steps in Microsoft Windows. The steps may be slightly different in Mac or Linux.

--

--

--

A nature-enthusiast appreciating technology, and a technology-obsessed exploring nature.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

“Finding Stuff” — Building a Great Search Experience

Fish from the movie “Finding Dory” with the words “Finding stuff: principles of search” against a blue background

Why you shouldn’t use hexagonal architecture with Go

Turn Mutliple Excel Files into One with this 10-line Python code

Your first Selenium WebDriver Script

Three Qualities that helped me to Increase my Freelancing Rate from 5$ an Hour to 50$ an Hour

Useful Mac Shortcuts in VS Code

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bishwarup Paul

Bishwarup Paul

A nature-enthusiast appreciating technology, and a technology-obsessed exploring nature.

More from Medium

How to install, setup and get started with RStudio IDE on your Windows 10 device…

Using globals and locals from STATA in Python

SAS vs R: Which Is The Difference And Which One Is Better?

Side-by-side Data Wrangling Script in R and Python