R Markdown Notebook in VS code

I am looking for a general-purpose editor that can integrate and customize different features across all programming languages that I often use (e.g., R, Python, Julia and Javascript), and Visual Studio Code seems to be the best candidate for me. Here I will give a quick overview of the key features for deploying my R markdown notebook in VS code. These settings can be easily generalized to other languages by adjusting the engines/compilers or adding the language-specific extensions.

Before I get started, it is necessary to install some extensions.

1. Install Radian Console

It is recommended to use the radian console as an alternative, since it supports rich syntax highlight for R script. You can directly install radian in your terminal via .

With radian installed, you still need to specify the R path in your VS code settings. Just search for “r.path” in your settings (cmd + ,), and add the path (/usr/local/bin/radian) in specific cell. To see whether it works well, you can restart VS code and search for “create R terminal” in your command template (shift + cmd + p). You can also set a shortcut for this, as shown in the figure below.

Specify the path and keybinding for radian console

2. Chunk Navigation

It is quite straightforward to navigate between and within code chunks in VS code, but I am more interested in combining basic chunk movements (run current, previous or next chunks) with some additional adjustments, such as placing the cursor at the end of a chunk after the execution. This requires multiple command combinations via the multi-command extension. Here I just cover some chunk navigations that I often use, and you can also design your own movements.

(1) Insert a R code chunk

For inserting a R code chunk, you can make use of the code snippet in VS code by simply typing “rc” and Enter. Alternatively, you can create a key binding (cmd+shift+i) to insert code snippet by putting the following piece inside your file.

(2) Move between chunks

To go to previous or next code chunks, I designed a series of movements by (i) moving to previous/next chunk and (ii) centering the whole window at the current cursor position. To achieve this, you can first define your movements and bind these movements with specific shortcuts. For example, you can define the following multi-commands in your (search for "open settings" in the command template).

Next, I set two key combinations for them (e.g., cmd + B for going to the chunk before, and cmd + N for next chunk).

Define shortcuts for multi-commands

(3) Move inside chunks

It is also useful to move inside a chunk, say quickly go to the beginning or end of a chunk, which is particularly helpful for me when one chunk is long. I did not see a direct command for this. The most intuitive way for me is to first select the whole chunk and move your cursor up or down a bit. You can directly append the following code in the scope of “multiCommand.commands” in your , and bind them with some shortcuts (cmd+A for beginning of a chunk and cmd+E for end of a chunk).

(4) Run current, previous and next chunk

The most important commands for me are the executions of current, previous and next code chunk. My personal preference is to run the current chunk and put the cursor at the end of the chunk. To do this, I simply follow the same steps by defining the movements in the and shortcuts.

(5) Fold and unfold a chunk

To fold and unfold a code chunk, I directly use the default settings in VS code. With the key binding of alt+cmd+left-bracket, you can fold the current chunk, while you can unfold it via alt+cmd+right-bracket.

3. View Objects

There is an experimental feature called that allows us to view different objects (vector, matrix, data.frame and list), plots, documentations and source code of functions in a separate window. This provides us a lot of handy usages to quickly preview the output of a piece of code. But before that, you need to create a file called in the home directory of your computer, and add the following settings. The purpose for this configuration is to pop up a separate window/tab beside your current editor. The default setting will open a mini window, but I prefer to have a separate window to preview each object.

(1) View an object (data.frame or list)

I set a shortcut (cmd+u) for viewing an object at the current selection or word. You can achieve this by adding the following chunk in the file (search for "open keyboard shortcuts" in the command template to open it).

Preview a data frame

(2) View a plot

This setting also allows you to preview a plot, though the size of the plot is fixed. I do not have a good solution for this, since cannot directly parse the figure settings in the header of Rmd code chunk. A temporary solution is to add the settings manually via options() at the beginning of a chunk, plus a global setting inside the , as shown above.

Preview a plot

(3) View function documentation

This also supports a fast preview of function descriptions by adding the following configuration in with a shortcut of F1.

Preview a function

(4) View source code

Similarly, we can also easily view the source code of a function by typing the shortcut of cmd+u.

Preview source code of a function

Programming, Data analysis & Deep learning!

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