Home

R Coding in VS Code via Docker Container

Ever tried to share your R code with a colleague, only to spend hours debugging “but it works on my machine” issues? Docker containers are like shipping containers for code—they package your R environment, libraries, and dependencies into a sealed box that works the same everywhere. Plus, you get access to tens of thousands of pre-built images on Docker Hub, where software developers publish ready-to-use environments, skipping the pain of manual software installation. This tutorial shows you how to run R in an isolated, reproducible environment using VS Code and Docker Desktop.

Key Concepts

What You’ll Need

Step 1: Install Docker Desktop

Step 2: Install Dev Containers Extension

Step 3: Clone the Vibe Project with GitHub Desktop

Step 4: Open Project in VS Code

Step 5: Reopen in Container

Note: The container automatically includes the R extension and languageserver package. The Dockerfile and devcontainer.json handle this for you.

Step 6: Understand the Container Environment

Now you’re coding inside a Linux container. Let’s explore what this means.

pwd

You’ll see /workspaces/vibe - this is your project folder inside the container.

ls

You’ll see the same files from the project: R/, .devcontainer/, README.md, etc.

cd ..
ls

You’ll only see vibe/ - the container is isolated. You can’t access your computer’s other folders, Desktop, or Documents. This isolation ensures your R environment is clean and reproducible.

cd vibe

Step 7: Run R Code Line by Line

The container has R pre-installed with common packages. Let’s run a simple data analysis script.

Step 8: Run the App

The project includes a demo Shiny app that creates an interactive histogram.

Step 9: Make a Simple Change

Let’s modify the app to see how development works.

titlePanel("My First R Docker App")

Step 10: Understanding the Dockerfile (Optional)

# choose a Dockerhub base image
FROM rocker/shiny-verse:latest

# 1. System deps commonly needed by R packages
RUN apt-get update && apt-get install -y \
    libcurl4-openssl-dev libssl-dev libxml2-dev git curl && \
    rm -rf /var/lib/apt/lists/*

# 2. R packages for VS Code integration: language server + debugger
RUN R -q -e 'install.packages(c("rstudioapi", "languageserver"), repos="https://cloud.r-project.org")'

# 3. Install Node.js LTS from NodeSource
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - \
    && apt-get install -y nodejs \
    && npm install -g npm@latest
    
# 4. Install Claude Code globally
RUN npm install -g @anthropic-ai/claude-code

# 5. Expose Shiny server port
EXPOSE 3838

Key parts:

Other Rocker images you can use:

After changing the base image, rebuild the container to apply changes.

Step 11: Install R Packages in the Docker Image (Optional)

Packages installed via the R console (install.packages()) are temporary and disappear when you rebuild the container. To make packages permanent, add them to the Dockerfile.

library(data.table)

If it loads without errors, the package is installed permanently.

Next Steps

Troubleshooting

Workflow Overview

This setup gives you a professional R development environment:

Everyday Workflow

Once everything is set up, here’s your daily routine:

  1. Start Docker Desktop - Open the app and wait for the green status indicator (Docker must be running)
  2. Open VS Code - Launch VS Code and open your project folder
  3. Reopen in Container - If not already in the container, click the green icon (bottom-left) and select Reopen in Container
  4. Write and run code - Edit .R files, run line-by-line with Ctrl+Enter/Cmd+Enter, or run Shiny apps with the ▶ Run Shiny App button
  5. Save your work - Your code files (.R, .Rmd) are saved to your computer and persist across sessions
  6. Commit and push - Use GitHub Desktop to commit your changes and push to the repository

Created by Steven Ge on December 7, 2025.