Home

Run R in VS Code

You want to write R code but RStudio feels heavy or you prefer VS Code’s flexibility. Think of VS Code as a Swiss Army knife - it can handle R, Python, and many other languages in one lightweight editor. This tutorial shows you how to set up R in VS Code with smart features like code completion, interactive plots, and even Shiny apps.

Key Concepts

What You’ll Need

Step 1: Install or Update R

You need R version 4.0 or higher for the best compatibility.

You’ll configure VS Code to find R in Step 3, so don’t worry if R --version doesn’t work in your terminal yet.

Step 2: Install R Extensions in VS Code

Step 3: Find Your R Installation Path

Before configuring VS Code, you need to know where R is installed on your system.

For Windows Users

  1. Open File Explorer
  2. Navigate to C:\Program Files\R\
  3. You’ll see a folder like R-4.5.3 (your version number may differ)
  4. Open that folder → Open the bin folder
  5. You’ll see R.exe there
  6. The full path is: C:\Program Files\R\R-4.5.3\bin\R.exe

Write down or remember your version number (e.g., R-4.5.3) - you’ll need it in Step 4.

For macOS Users

Your R installation is typically in one of these locations:

To verify which path you have, open Terminal and type:

which R

This will show you the exact path to your R installation.

For Linux Users

R is typically installed at: /usr/bin/R

Verify by opening a terminal and typing:

which R

Step 4: Configure VS Code to Find R

For Windows, add this line (replace R-4.5.3 with your version from Step 3):

"r.rpath.windows": "C:\\Program Files\\R\\R-4.5.3\\bin\\R.exe",

Why two backslashes (\\)? In JSON files, the backslash \ is a special character (called an escape character). To represent a single backslash in the actual file path, you must type two backslashes \\. So C:\Program Files becomes C:\\Program Files in settings.json.

For macOS, add one of these lines:

"r.rpath.mac": "/Library/Frameworks/R.framework/Resources/bin/R",

Or if you installed R via Homebrew on Apple Silicon:

"r.rpath.mac": "/opt/homebrew/bin/R",

For Linux, add:

"r.rpath.linux": "/usr/bin/R",

Step 5: Install Required R Packages

install.packages("languageserver")
install.packages("shiny")

Simply typing R in a regular terminal won’t work unless R is in your system PATH. The R extension uses your r.rpath setting to find R.

Step 6: Create Your R Project

Step 7: Write Your First R Script

# Load the iris dataset
data(iris)

# View the first few rows
head(iris)

# Generate summary statistics
summary(iris)

hist(iris$Sepal.Length)

Step 8: Run R Code Interactively

Step 9: Create a Simple Shiny App

library(shiny)

ui <- fluidPage(
  titlePanel("Interactive Histogram"),

  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 5,
                  max = 50,
                  value = 30)
    ),

    mainPanel(
      plotOutput("histogram")
    )
  )
)

server <- function(input, output) {
  output$histogram <- renderPlot({
    x <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    hist(x, breaks = bins, col = "steelblue", border = "white",
         xlab = "Waiting time (minutes)",
         main = "Distribution of Waiting Times")
  })
}

shinyApp(ui = ui, server = server)

Step 10: Use Code Completion and Hover Help

Step 11: Try More Features

Next Steps

Troubleshooting

Workflow Summary

VS Code provides a modern, lightweight alternative to RStudio with these key advantages:


Created by Steven Ge on December 7, 2025.