Claude Code in RStudio
Most of you write R, not Python. This tutorial gets Claude Code working inside your normal RStudio workflow — with a short detour to explain Node.js, which you may or may not actually need to install.
1. Do I need Node.js? (Probably not.)
What is Node.js?
Node.js is a runtime that lets your computer execute JavaScript code outside
a web browser. Many cross-platform developer tools — including a lot of MCP servers
(we'll get to those) and the old way of installing Claude Code — are written in
JavaScript and need Node.js in order to run. The Node.js install also gives you a command-line
tool called npm ("Node Package Manager"), which is the JavaScript equivalent
of pip for Python or install.packages() for R.
The honest truth about Claude Code and Node.js
If you install Claude Code via the recommended methods, you do not need Node.js installed at all:
| Install method | Needs Node.js? |
|---|---|
Native installer (curl ... | bash on macOS/Linux,
irm ... | iex on Windows) | No |
Homebrew (brew install --cask claude-code) |
No |
WinGet (winget install Anthropic.ClaudeCode) |
No |
Linux apt / dnf / apk |
No |
npm (npm install -g @anthropic-ai/claude-code) |
Yes — Node.js 18+ |
So when do I want Node.js?
Three realistic reasons in this course:
- Some MCP servers are distributed as npm packages and are launched with
npx. If you ever want to add, say, the GitHub MCP server or the Notion MCP server to Claude Code, you'll need Node.js installed. - You like the
npminstall path for Claude Code itself because you already use Node tools. - You want to update Claude Code through a single package manager you already trust.
For our purposes — running Claude Code with R, using the R MCP server — you
do not need Node.js. The R MCP server we'll use (posit-dev/mcptools)
is pure R and is launched with Rscript.
Installing Node.js
macOS
# Recommended: Homebrew
brew install node
# Verify
node --version # v22.x.x or later
npm --version # 10.x.x or later
If you don't have Homebrew, install it first with the command at
brew.sh, then run brew install node.
Windows
# PowerShell — works on Windows 10 / 11
winget install OpenJS.NodeJS.LTS
# Close and re-open PowerShell so PATH refreshes, then:
node --version
npm --version
Alternatively, the official MSI installer from nodejs.org/en/download — pick the LTS ("Long Term Support") download for your OS. Accept all defaults.
Linux (Ubuntu/Debian)
# The Ubuntu repo version is usually too old. Use NodeSource's installer:
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs
node --version
npm --version
Why "LTS"?
Node.js has two release tracks: Current (newest features, breaks more often) and LTS (stable, supported for ~30 months). Unless you have a specific reason, pick LTS. It's what almost every MCP server author tests against.
Verifying npm can install global packages
Sometimes (especially on Linux), npm wants admin rights to install global packages, which is a bad idea. To make sure it'll install into your user folder instead:
mkdir -p ~/.npm-global
npm config set prefix ~/.npm-global
# Add to your shell startup so the `npx` command from globally-installed
# packages is on PATH:
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.zshrc # or ~/.bashrc
source ~/.zshrc
On Windows the WinGet installer handles this automatically.
sudo npm install -g. It creates permission tangles that are painful to
undo. If you hit a permission error, fix the prefix as above rather than reaching for
sudo.
2. Two ways to use Claude Code with RStudio
There are two routes that will cover essentially everything you need to do in this course:
| Route | What it does | When to use |
|---|---|---|
| A — RStudio terminal pane | Run claude in RStudio's built-in terminal. Claude reads and edits your
R scripts directly, and can run them with Rscript. |
Default; works immediately after installing Claude Code. |
B — mcptools R MCP server |
Adds R as a tool Claude can call. Claude can execute R code in your live R session, see your data frames, and inspect plots without re-loading data. | When you want Claude to interact with the same R session you're working in — especially for exploratory analysis and modeling. |
Most students start with route A and add route B once they're comfortable. We'll set up both.
3. Route A — the RStudio terminal pane
Step 1: install Claude Code (no Node.js required)
If you haven't already, follow the install instructions on the Claude Code Setup tutorial. The short version:
# macOS
brew install --cask claude-code
# Windows (PowerShell)
winget install Anthropic.ClaudeCode
# Linux / WSL
curl -fsSL https://claude.ai/install.sh | bash
Verify with claude --version in any terminal.
Step 2: open RStudio's terminal pane
RStudio has a Terminal tab next to the Console at the bottom of the window. If you don't see it: Tools → Terminal → New Terminal (or Alt+Shift+R on Windows, Option+Shift+R on macOS).
The terminal opens in your current RStudio project directory. To confirm:
pwd # should print your project path
ls # should list your .R, .Rproj, data/ folder, etc.
Step 3: launch Claude Code
claude
You're now in an interactive Claude Code session inside your R project. Claude can read every file in this folder.
Step 4: try a real task
Type:
"Readanalysis.R. Add a goodness-of-fit test after theoccu()call usingunmarked::mb.gof.testwith 200 bootstrap reps. Save the result toresults/gof.rds. Run the script with Rscript when you're done."
Claude will:
- Show you the proposed diff to
analysis.Rand ask for approval. - Once approved, write the new code.
- Ask permission to run
Rscript analysis.R. - Show you the stdout/stderr and the new file it created.
That's the whole loop. You stay in RStudio for editing and reviewing diffs; Claude does the repetitive work.
Tools → Reload All Open Files, to pick up
the changes.
Step 5: pre-approve safe commands
So you're not clicking "Allow" every time Claude wants to run Rscript, edit
~/.claude/settings.json (or, per-project, .claude/settings.json at
your project root):
{
"permissions": {
"allow": [
"Bash(Rscript *)",
"Bash(R CMD *)",
"Bash(git status)",
"Bash(git diff *)",
"Bash(ls *)"
]
}
}
4. Route B — let Claude run code in your live R session via mcptools
What is mcptools?
mcptools (by Posit, the makers of RStudio) is an R package that runs an MCP
server in R. Once configured, Claude Code can:
- Execute arbitrary R expressions in your active R session,
- Read your data frames, models, and other in-memory objects,
- Get the output of
summary()/str()/head()/plot()on objects you've already loaded, - Install packages (with permission).
This is much more interactive than route A — Claude doesn't have to re-read your data from disk every time, and it can see exactly what you're seeing in the R console.
Step 1: install mcptools
In your RStudio Console:
# CRAN release
install.packages("mcptools")
# Or the development version
# install.packages("pak")
# pak::pak("posit-dev/mcptools")
Step 2: register the MCP server with Claude Code
Edit your Claude Code settings file. On macOS/Linux this is ~/.claude.json;
on Windows it's %USERPROFILE%\.claude.json. Add or extend the
mcpServers block:
{
"mcpServers": {
"r": {
"command": "Rscript",
"args": ["-e", "mcptools::mcp_server()"]
}
}
}
If Rscript isn't on your PATH (common on Windows), give the full path:
{
"mcpServers": {
"r": {
"command": "C:\\Program Files\\R\\R-4.5.0\\bin\\Rscript.exe",
"args": ["-e", "mcptools::mcp_server()"]
}
}
}
Step 3: attach your live R session
In the R session you want Claude to be able to drive (the one running inside RStudio), register it:
library(mcptools)
mcp_session()
You'll see a message confirming the session is registered. Leave the session open. Claude
Code (started in a different terminal — e.g., the RStudio Terminal pane) will now see
a tool called something like r_run or r_session in its tool
list.
Step 4: try it
In RStudio's Console, load some data:
library(unmarked)
data(crossbill)
crossbill_uf <- unmarkedFrameOccu(y = crossbill[, 6:8], siteCovs = crossbill[, 1:5])
Now, in the Claude Code session (in the RStudio Terminal pane), say:
"There's a registered R session. Look at the object crossbill_uf
in it, summarise its structure, then fit a constant-psi, constant-p single-season occupancy
model. Report the back-transformed psi and p with 95% Wald CIs."
Claude will call summary(crossbill_uf) and occu(~1 ~1, crossbill_uf)
inside your R session, see the results, back-transform them, and report. No data
leaves your laptop.
5. The recommended workflow for the labs and final project
For Labs 9 and 10 and most modeling work in the final project:
- Open the project in RStudio (your
.Rprojfile). - Open the Terminal pane.
- Run
claudein the terminal and put it in plan mode (Shift+Tab). - In the R Console,
library(mcptools); mcp_session()if you want route B. - Drive the analysis from Claude. Review every diff in the RStudio Source pane before accepting.
- Commit often (see the Git tutorial); Claude can stage and commit too.
6. RStudio-specific gotchas
| Symptom | Cause | Fix |
|---|---|---|
| Editor still shows old code after Claude edits a file | RStudio doesn't auto-reload disk changes. | Tools → Reload All Open Files, or close and reopen the tab. |
Rscript: command not found on Windows |
R's bin directory isn't on PATH. |
Either add it (System Properties → Environment Variables) or
give the absolute path in ~/.claude.json. |
| Claude can't see your in-memory objects via route B | You didn't call mcp_session(), or you called it in a different R
process. |
Call it in the active RStudio Console, the one whose objects you want Claude to read. |
"npx ..." errors when adding an MCP server |
Node.js isn't installed. | Install Node.js (section 1). Not needed for the R MCP server. |
| Slow first run of Claude Code in RStudio terminal on Windows | Defender / antivirus scanning the binary. | Wait it out once; subsequent launches are fast. |
7. What about the RStudio "Posit Assistant" / Copilot Chat?
RStudio (now Posit Workbench) has its own AI assistance features, including GitHub Copilot integration. You can use them alongside Claude Code — they don't conflict — but they're for line-completion and chat, not the autonomous multi-step work Claude Code is designed for. For the labs in this course, use Claude Code.