Claude-R - Execute R through RStudio
Connect an AI coding assistant (Claude Code or OpenCode) to RStudio, enabling agentic R programming. RStudio acts as the execution engine; the assistant generates and runs the code.
OpenCode discovers this skill from .claude/skills/ just like Claude Code (docs). The MCP config is written to .mcp.json (Claude Code) or opencode.jsonc (OpenCode).
Note: All commands in this document assume the working directory is the project root.
Quick Start
Prerequisites Check
Verify RStudio server is running:
uv run .claude/skills/claude-r/scripts/check_r_connection.py # or with system Python (if httpx installed) python .claude/skills/claude-r/scripts/check_r_connection.py
If not running:
- •Open RStudio
- •File -> New Project -> Existing Directory -> select the project folder -> Create Project (If .Rproj already exists, instead: File -> Open Project -> select your .Rproj file -> Open)
- •Run:
library(ClaudeR) - •Run:
claudeAddin() - •Click "Start Server" in Viewer pane
Basic Usage
Execute R code:
x <- 1:10 mean(x)
Generate plots:
library(ggplot2) ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point(size = 3) + theme_minimal()
Available MCP Tools
execute_r
Execute R code and return text output.
execute_r_with_plot
Execute R code that generates graphics. Returns PNG image.
get_r_info
Query R environment:
- •
what: "packages"- Installed packages - •
what: "variables"- Workspace objects - •
what: "version"- R version - •
what: "all"- Complete summary
get_active_document
Retrieve currently open file in RStudio.
modify_code_section
Modify code in active RStudio document by pattern or line range.
Setup and Installation
Step 1: Install R and Dependencies
bash .claude/skills/claude-r/scripts/setup_claude-r.sh
This installs:
- •R via Homebrew
- •System dependencies (harfbuzz, libtiff, libgit2, etc.)
- •R packages (renv, devtools, ClaudeR)
Step 2: Configure MCP Client
- •
Get the MCP server script path:
bashRscript -e 'cat(system.file("scripts/persistent_r_mcp.py", package="ClaudeR"))' - •
Determine which client the user is using. If unclear, ask the user.
- •
Register the MCP server:
Claude Code (
claude mcp addwrites to.mcp.json):With
uv(recommended, no pre-install needed):bashclaude mcp add -s project r-studio -- \ uv run --with httpx --with mcp python <script path>
With system Python (install
httpxandmcpfirst):bashpython3 -m pip install --user httpx mcp claude mcp add -s project r-studio -- \ python3 <script path>
Docs: https://code.claude.com/docs/en/mcp
OpenCode (write
opencode.jsoncin the project root):With
uv(recommended):jsonc{ "$schema": "https://opencode.ai/config.json", "mcp": { "r-studio": { "type": "local", "command": [ "uv", "run", "--with", "httpx", "--with", "mcp", "python", "<script path>" ], "enabled": true } } }With system Python:
jsonc{ "$schema": "https://opencode.ai/config.json", "mcp": { "r-studio": { "type": "local", "command": ["python3", "<script path>"], "enabled": true } } }
Step 3: Start R Server
- •Open RStudio
- •File -> New Project -> Existing Directory -> select the project folder -> Create Project (If .Rproj already exists, instead: File -> Open Project -> select your .Rproj file -> Open)
- •
library(ClaudeR) - •
claudeAddin() - •Click "Start Server"
Step 4: Verify
Claude Code: Run /mcp in Claude Code, or claude mcp list from the terminal. "r-studio" should show "connected".
OpenCode: Run /mcps in OpenCode, or opencode mcp list from the terminal. "r-studio" should show "connected".
Architecture
Three-layer system:
- •AI Coding Assistant (Claude Code or OpenCode): Natural language interface
- •MCP Server: Python bridge (stdio ↔ HTTP)
- •ClaudeR HTTP Server: R execution in RStudio (port 8787)
RStudio acts passively. Users interact with the assistant, which generates and executes R code. RStudio's Environment, Plots, and Console panes show results.
See references/architecture.md for details.
Troubleshooting
If R code execution fails, check connection:
uv run .claude/skills/claude-r/scripts/check_r_connection.py
Common issues:
- •RStudio server not running
- •MCP configuration incorrect
- •Python dependencies missing
See references/troubleshooting.md for complete guide.
Key Features
- •State persistence: R session persists across executions
- •RStudio integration: View variables, plots, and console in RStudio
- •Agentic: Generate and execute R code through conversation
Limitations
- •Single-threaded: Sequential execution only
- •Local only: Server binds to localhost
- •RStudio required: Must be running with server active
- •No streaming: Results return after complete execution
Resources
- •Troubleshooting Guide - Connection issues, setup errors
- •Architecture - System design and data flow
- •Setup Script - Automated installation
- •Connection Checker - Verify server status
- •ClaudeR GitHub - Original project