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.
- Install R (download and install from CRAN).
- 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
binfolder inside the installed R folder (such as
C:\Program Files\R\R-4.1.0\bin) to the Path variable.
- 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.
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.
The Command Palette should automatically open when all repositories are loaded, showing you an alphabetical list of extensions you can install.
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.
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.
After SendCode is installed, open the Command Palette and search ‘SendCode’. Select the option that says ‘SendCode: Choose Program’ and hit
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.
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
This will open a 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.
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.
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.
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)’.
This will open the Terminus window in the right column, instead of the bottom.
Now you can open R in this right pane, and see the code and output side by side.
Declaration: I have shown the steps in Microsoft Windows. The steps may be slightly different in Mac or Linux.