Getting Started

The best guild to follow is this one provide on the official website of VS Code. The following steps are essential:

  • Install R
  • Install the languageserver packages, using install.packages("languageserver") in the R console.
  • Install the R Extension in VS Code.
  • You also want to install several other R packages such as rmarkdown, knitr.

Enhanced Features

VS Code allows more enhanced features, such as Copilot, which is a AI assistant that helps you write code. To enable this feature, you need to install the GitHub Copilot extension. However, this is a paid feature that requires a GitHub account and a subscription to GitHub Copilot. Please note that using this feature is not prohibited in this course, but you should be aware of the potential risks (whatever that might be) since this is an experimental feature.

Using R Markdown in VS Code

Using R Markdown in VS Code is very similar to using it in RStudio. You can create a new R Markdown file by clicking the New File button on the top left corner of the VS Code window, and then select R Markdown from the drop-down menu. You can also create a new R Markdown file by clicking File -> New File -> R Markdown.

This paragraph above was actually generated by Copilot with the prompt Using R Markdown in. And it didn’t mention that you can get a free subscription with the GitHub education program as a student.

Writing R Code (with Copilot)

Here’s an example of a code chunk that plots the iris dataset. Again the code was completed by Copilot.

  # load the iris dataset
  data(iris)

  # plot the iris dataset, with color indicating species
  # and also use solid dots
  plot(iris$Sepal.Length, iris$Sepal.Width, 
       main = "Iris Dataset", 
       xlab = "Sepal Length", ylab = "Sepal Width",
       col = iris$Species, pch = 19)

However, I did a few attempts before I got the code I want. Hence, you should always check the code generated by Copilot before you run it. For example, I wanted to generate a data matrix with correlated data such that the covariance matrix has 0.5 on the off-diagonal entries. Copilot failed this task.

  # Generate a data matrix with 100 rows and 4 columns
  # with covariance matrix 0.5 on off-diagonal entries
  # and 1 on diagonal entries
  set.seed(123)
  x <- matrix(rnorm(400), nrow = 100, ncol = 4)
  x <- x %*% t(x)
  diag(x) <- 1
  x <- x / max(x)

Instead the corrected code should be

  # define a matrix with 0.5 on off-diagonal entries and 1 on diagonal entries
  S <- matrix(0.5, nrow = 4, ncol = 4)
  diag(S) <- 1

  # generate 100 random samples from a multivariate normal distribution
  # with mean 0 and covariance matrix S
  set.seed(123)
  x <- MASS::mvrnorm(100, mu = rep(0, 4), Sigma = S)

  # calculate the sample covariance matrix
  cov(x)
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.9137519 0.3503305 0.4356605 0.4680046
## [2,] 0.3503305 0.7738285 0.3197805 0.4123654
## [3,] 0.4356605 0.3197805 0.9108208 0.4093817
## [4,] 0.4680046 0.4123654 0.4093817 0.9428806

Potential Issues

There were many issues I have encountered while using VS Code for RMarkdown files (I am using windows). At the beginning it could be frustrating, but after a few tries, I was able to solve most of them. Here are some of the issues I have encountered and how I solved them. You may encounter different issues. If you are not able to solve the problem, I would recommend switching to RStudio.

  • System path. This is probably one of the most common one. The solution is just to include the correct path of R
  • Necessary packages. Make sure to follow the instruction on the official website to install the necessary packages.
  • Extensions. You may found that your setup is different than mine. For example, I use the Live Preview extension for visualizing the rendered HTML file.
  • VS Code settings. This can also be frustrating. For example, when I setup this computer, my knit button does not successfully render the HTML file. It always stuck at 0%. But this never happened on my desktop. However, after googling similar issues, I turned off Background Process and it was able to render. I have not found a better solution yet. If you know a better solution, please let me know.
  • radian is a modern R console. To install this feature, you also need python. A detailed instruction is provided at their github page. Personally, I had many issues related to radian in windows. So I would say this is optional. For example, sometimes radian will add an additional bracket to the line you submit to the console. This is a known issue and you can either just delete the bracket, or you can setup a radian profile with the following line options(radian.auto_match = FALSE).