全库搜索 Markdown 图册
Paper Analysis · 21 分钟阅读

ClaudeR/clauder-mcp at main

站内编号 KB-000096
clippingsMCPRStudioClaude_CodeCodexmulti_agentReviewer_ZeroAI_workflow已试用
板块与文章导航

摘要LLM 归库摘要

  • 核心内容: ClaudeR 是一个 R 包,通过 MCP 协议在 RStudio 和 LLM 代理(Claude Code、Codex、Qwen Code、Gemini CLI)之间建立双向连接。支持交互式编码、多代理编排、自动稿件审核(Reviewer Zero)、数据标注等功能。提供 execute_r、execute_r_async、search_project_code、verify_references 等 20+ MCP 工具。安装方式为 devtools::install_github("IMNMV/ClaudeR"),支持 uvx 零配置启动。
  • 可复用知识: 涵盖 AI 驱动的学术审核协议(Reviewer Zero 4 步扫描:提取→验证→重新计算→参考文献检查)、多代理协调协议(共享消息板、任务认领、交叉检查)、R 最佳实践协议(EDA→假设检验→模型构建→诊断→报告)。这些协议可直接用于其他 AI 辅助科研场景。
  • 关联页面建议:
    • 04 AI_API与自动化库/02_wiki/mcp/MCP 工具开发与使用(ClaudeR 是 MCP 服务器在科研场景的典型实践)
    • 04 AI_API与自动化库/02_wiki/skills/(其 Reviewer Zero 协议可启发 skill 设计)
  • 下一步处理: 可实测安装 ClaudeR 包并试用 Reviewer Zero 功能;其多代理编排协议值得深入阅读并可能适配到当前 workflow。
  • 归库说明: 分类到 04 AI_API与自动化库,置信度 0.85,理由:标题含 clauder-mcp;正文集中介绍 MCP/Claude Code/Codex/Qwen/Gemini 等多代理 AI 工具集成;无 DOI/PMID 论文信号。

ClaudeR Logo

ClaudeR - 现代研究人员工具包#

将 RStudio 连接到 Claude Code、Codex、Qwen Code、Gemini CLI 或任何基于 MCP 的 LLM 代理,以实现交互式编码、多代理编排、自动稿件审核和数据标注。


ClaudeR 是一个 R 包,它能在 RStudio 和配置了 MCP 的 LLM 代理(例如 Claude Code、Codex 或 Qwen Code)之间建立直接连接。这使得交互式编码会话成为可能,代理可以在您当前使用的 RStudio 环境中执行代码,从而实时查看执行的代码和生成的图表。如果您需要帮助编辑脚本、进行快速分析,或者需要 LLM 在提交稿件前审核您的统计结论,ClaudeR 都能为您提供支持。

此外,该软件包还允许多个代理同时处理同一个脚本,或者可以将多个 RStudio 窗口隔离,以便多个代理可以独立处理不同的数据集。它还兼容 Cursor 以及任何支持 MCP 服务器的服务。

快速入门#

# Install
if (!require("devtools")) install.packages("devtools")
devtools::install_github("IMNMV/ClaudeR")

# Set up your AI tool
library(ClaudeR)
install_clauder()          # For Claude Desktop / Cursor
install_cli(tools = "claude")  # For Claude Code CLI

# Start the server in RStudio
claudeAddin()

AI 代理: 有关自动化安装说明,请参阅 llms-install.md 。

最新动态 (点击展开)

  • AI-Driven Data Annotation. Five MCP tools (load_annotation_data, annotate, run_annotation_job, get_annotation_job_status, cancel_annotation_job) let an agent label a CSV dataset row by row without writing any code. Two modes: interactive (context accumulates across rows) or fully-isolated via run_annotation_job (one fresh claude, codex, gemini, or qwen subprocess per row, or a local ollama HTTP call for free, private, offline labeling). Codex jobs accept a reasoning_effort parameter; Ollama jobs accept an ollama_base_url parameter. The original file is never modified and sessions resume automatically if interrupted.
  • Multi-Agent Coordination Protocol. Built-in protocol for multiple agents sharing one RStudio session. Agents negotiate through a shared message board in the R environment, agree on a task plan, claim tasks before working, and cross-check each other's output. Load it with multi_agent_prompt().
  • verify_references tool. Extracts DOIs from a manuscript's bibliography, queries the CrossRef API for each, and returns metadata (title, authors, year, journal) for comparison against manuscript claims. Non-resolving DOIs, metadata mismatches, and references without DOIs are flagged. Works standalone ("check my references") or as Pass 4 of Reviewer Zero.
  • R Best Practices Protocol. Built-in statistical analysis protocol covering EDA, assumption checking, model building, diagnostics, multiple-corrections, and reporting. Load it with r_best_practices_prompt() or tell the agent to read it.
  • Reviewer Zero: Automated Academic Audits. Now a 4-pass protocol for AI-driven manuscript verification. The agent extracts every statistical and methodological claim, verifies its extraction, recomputes values against the author's R code, and checks references via CrossRef. Methodological claims (e.g., "zero variance made testing impossible") are tested directly rather than accepted at face value. Run reviewer_zero_prompt() to get the full protocol.
  • clean_error_log tool. Point the agent at a session log and it will parse every code block, find errors, check whether a fix follows each one, then strip the error blocks and any duplicate code that preceded them. The result is a clean log with only the working code. Accepts an optional output_path to write to a separate file instead of overwriting the original.
  • Persistent server across UI restarts. Closing the Shiny addin (console stop or Done button) no longer kills the MCP server. Re-running claudeAddin() reconnects to the still-running server with the correct port, session name, and execution count. Only clicking "Stop Server" in the UI actually stops the server.
  • Descriptive log filenames. Log files now include the session name, port, and timestamp: clauder_default_8787_20260301_143022.R. A new log file is created each time you click Start Server. All subsequent code execution appends to that file.
  • Viewer content capture & insert_text tool. Two new tools: get_viewer_content reads HTML from interactive widgets (plotly, DT, leaflet) with pagination so agents can inspect htmlwidget output without blowing up context. insert_text inserts text at the cursor position or a specific line/column in the active document. During agent execution, htmlwidgets open in the browser instead of stealing the Shiny addin's viewer pane.
  • Multi-session routing fix. Agents now prefer the session named "default" when multiple sessions are active, preventing misrouting caused by non-deterministic discovery order. Once bound, agents stay sticky to their session. Non-default agents should call connect_session to target a specific session.
  • Reproducibility metadata in logs. When logging is enabled, each new session log starts with a header containing the date, working directory, and full sessionInfo() output (R version, platform, attached packages). Anyone who receives the log can see exactly what environment the code ran in.
  • Export clean script. Click "Export Clean Script" in the Shiny addin to strip all timestamps, agent labels, and log headers from a session log, producing a runnable .R file with just the code. Error blocks are preserved as comments. Also available programmatically via export_log_as_script().
  • PyPI package (clauder-mcp). The Python MCP bridge is now available as a standalone package on PyPI. Run it with uvx clauder-mcp for zero-config setup with no Python path or pip install needed. The installers (install_cli() and install_clauder()) default to uvx, with a use_uvx = FALSE fallback for legacy setups.
  • read_file tool. Agents can now read any text file from disk (.R,.qmd,.csv,.log, etc.) without it being open in RStudio. Enables session continuity workflows: point an agent at a previous log file and tell it to pick up where the last session left off.
  • Codex + Qwen Code CLI support. install_cli(tools = "codex") and install_cli(tools = "qwen") generate setup commands for OpenAI Codex and the Qwen Code CLI (Alibaba's gemini-cli fork). Both join Claude Code and Gemini as supported CLI agents.
  • Multi-agent orchestration. Run multiple AI agents on the same R session or spread them across separate RStudio windows. Each agent gets a unique ID on startup. Console output, log files, and execution history are all attributed per agent, so you always know who did what. On its very first tool call, each agent receives a context briefing with its own ID, any other agents active on the session, and the log file path, giving it full awareness of the shared environment without any manual setup. Agents can call get_session_history to review what other agents have done, or read the shared log file directly. The Shiny viewer tracks connected agents in real-time.
  • Session discovery. Each RStudio session writes a discovery file to ~/.claude_r_sessions/ on startup. AI agents find sessions automatically with no hardcoded ports. Name your sessions (e.g. "analysis", "modeling") and run them on different ports. When multiple sessions exist, agents automatically route to the session named "default". Non-default agents should call connect_session to bind to their target session. Single-session setups work with zero config.
  • Redesigned Shiny viewer. Cleaner UI with grouped panels for Session, Agents, Logging, and Advanced settings. Shows connected agents and execution count in real-time. Click the ? button for a built-in guide on multi-session setup and agent identity.
  • Non-blocking async execution. execute_r_async now runs long-running code in a separate R process via callr, keeping the main session fully responsive. Other agents can continue working while a job runs. The agent writes self-contained code (explicitly saving/loading data via saveRDS), submits it, and polls with get_async_result. No environment copying and no memory doubling, only the data the job needs gets serialized.
  • Stale plot detection. Fixed a bug where the last generated plot image would persist and re-appear on every subsequent execute_r call, even when no new plot was created.
  • Reduced plot token usage. Plot capture now uses smaller dimensions (600x400, dpi 100) to reduce base64 image size and avoid token overflow errors.
  • MCP tool annotations. All tools now include readOnlyHint, destructiveHint, and idempotentHint annotations per the current MCP spec.
  • Hardened string escaping. escape_r_string now handles backticks, carriage returns, tabs, and null bytes. Applied to task tool inputs to prevent injection.
  • Fixed install_cli() command syntax. Updated to use --transport stdio flag and -- separator for current Claude Code CLI. Now removes stale MCP registrations before adding fresh ones, preventing issues when upgrading R versions.

演示#

通过 Claude 桌面应用程序进行单代理操作 多代理:Codex + Claude Code(通过 CLI) GPT 5.4 Codex:数据分析 + Quarto 报告
Single Agent Demo Multi-Agent Demo Codex Quarto Demo

目录#

特征#

ClaudeR 为您的 AI 助手提供了一套工具,使其能够与您的 R 环境进行交互:

  • execute_r :执行 R 代码并返回输出。
  • execute_r_with_plot :执行生成模型可以看到的图表的 R 代码。
  • execute_r_async :异步执行耗时较长的 R 代码(>25 秒)。返回一个用于轮询的作业 ID。
  • get_async_result :轮询异步作业的结果。内置延迟以限制轮询频率。
  • list_sessions :列出代理可以连接的所有活动的 RStudio 会话。
  • connect_session :通过名称连接到特定的 RStudio 会话,以进行多会话工作流程。
  • get_session_history :查看按代理 ID 筛选的执行历史记录。
  • read_file :无需在 RStudio 中打开,即可从磁盘读取任何文本文件(.R、.qmd、.csv、.log 等)。支持对大型文件进行 start_line / end_line 分页。
  • get_active_document :获取 RStudio 中活动文档的内容。
  • get_r_info :获取有关 R 环境的信息。
  • modify_code_section :修改活动文档中的特定代码段。
  • insert_text :在当前光标位置或活动文档中的特定行/列插入文本。
  • get_viewer_content :从查看器窗格(plotly、DT、leaflet 小部件)读取 HTML 内容,支持分页。
  • clean_error_log :通过删除错误块及其重复的前身来清理会话日志,只留下工作代码和后续的修复。
  • search_project_code :在项目源文件(.R、.Rmd、.qmd)中搜索正则表达式模式。返回文件名、行号和代码片段。
  • probe_scripts :在干净的后台会话中运行 R 脚本,并报告创建的对象(名称、类别、维度),而不会影响您的主会话。
  • verify_references :通过提取 DOI 并将其与 CrossRef API 进行比对来验证学术参考文献。返回用于比较的元数据(标题、作者、年份、期刊)。没有 DOI 的参考文献将被标记,以便进行人工网络搜索。
  • create_task_list :根据您的提示生成任务列表,以防止遗漏长上下文任务。
  • update_task_status :跟踪生成的列表中每个任务的进度。
  • load_annotation_data :加载用于标注的 CSV 文件。创建一个工作副本,解析 _schema 列,并显示第一个未标注的行。中断后可恢复。
  • annotate :注释当前行,根据模式进行验证,立即保存,并自动加载下一行。
  • run_annotation_job :在后台对整个 CSV 文件进行标注,行与行之间无上下文信息泄露。每一行都由一个全新的 claude 、 codex 、 gemini 或 qwen 进程进行评分,或者通过本地 ollama HTTP 调用进行评分(用于免费、私密、离线标注)。接受一个 reasoning_effort 参数( low 、 medium 、 high ,用于 Codex 标注)和一个 ollama_base_url 参数,用于指向远程 Ollama 服务器。
  • get_annotation_job_status :检查正在运行或已完成的标注作业的进度。
  • cancel_annotation_job :取消正在运行的后台标注作业。取消前已完成的行将保留在输出文件中。

借助这些工具,您可以:

  • 直接代码执行 :AI 可以在您当前 RStudio 会话中编写、执行和安装软件包。
  • 反馈与帮助 :获取 R 脚本的解释或请求对特定行进行编辑。
  • 可视化 :人工智能可以生成、查看和改进图表和可视化效果。
  • 数据分析 :让人工智能分析您的数据集并迭代提供见解。
  • 多代理工作流 :在同一个 R 会话中同时运行 Claude Desktop、Claude Code、Codex、Qwen Code 和 Gemini CLI。每个代理都有唯一的标识符,它们可以通过共享的历史记录和日志文件查看彼此的工作。
  • 长时间运行分析 :异步执行可处理模型拟合、模拟和大数据处理,而不会超时。
  • 代码日志记录 :将人工智能执行的所有代码保存到日志文件中,以供将来参考。每个条目都会标记执行它的代理。
  • 控制台打印 :在执行前将 AI 的代码打印到控制台。
  • 环境集成 :人工智能可以访问 R 环境中的变量和函数。
  • 动态摘要 :摘要可以动态地从对象和数据框中提取结果,以防止出现幻觉。
  • Quarto 渲染 :人工智能可以创建和渲染 Quarto 演示文稿。为了获得最佳效果,请提供.qmd 文件,并在完成后将其渲染为 HTML 格式。
  • Reviewer Zero :一套用于自动化学术审核的内置协议。该人工智能系统逐段读取稿件,将每一项统计结论提取到注册表中,验证提取结果,然后根据作者的 R 代码重新计算每一项结论。运行 reviewer_zero_prompt() 可查看完整协议。请参阅下方的 Reviewer Zero 部分。
  • 数据标注 :使用内置标注工具逐行标注 CSV 数据集。在 _schema 列中定义标注模式,运行 data_annotation_prompt() 获取协议,然后使用 run_annotation_job 以交互方式或完全隔离的逐行子进程模式进行标注。

Reviewer Zero:自动化学术审核#

ClaudeR 内置了一套基于人工智能的学术论文技术评审协议。人工智能充当"零号审稿人"的角色:系统地验证论文中的每个 p 值、系数和置信区间是否与生成它们的程序代码相符。

工作原理(4次扫描协议):

  1. 提取 :AI 使用分页 read_file 逐块读取您的手稿,将每个定量和方法论主张提取到结构化注册表中(在您的 RStudio 环境窗格中可见的数据框)。
  2. 验证 :人工智能会重新读取每条声明的源代码,以确认没有误读数值。所有声明都验证完毕后,代码才会运行。
  3. 重新计算 :人工智能使用 search_project_code 和 probe_scripts 查找相关的 R 脚本,然后执行 execute_r 重新运行分析,并将重新计算的值与原文进行比较。方法论上的断言(例如,"零方差使得测试无法进行")会直接进行检验,而不是简单地接受其表面含义。
  4. 参考文献 :人工智能使用 verify_references 从参考文献中提取 DOI,并逐一对照 CrossRef API 进行验证。元数据不匹配、DOI 无法解析以及缺少 DOI 的参考文献都会被标记出来。文中引用也会与参考文献进行交叉核对。

开始使用:

# Print the full protocol prompt to give to your AI agent
reviewer_zero_prompt()

该协议适用于 .docx 、 .pdf 、 .qmd 、 .Rmd 、 .tex 或纯文本稿件,并支持多脚本 R 项目。

R 最佳实践协议#

ClaudeR 内置了一套统计分析协议,其灵感来源于我从统计学课程中学到的建模工作流程,并结合我在实际统计工作中使用 AI 代理的经验教训不断完善。其目标是利用自然语言引导模型进行可复现的、理论驱动的分析,涵盖探索性数据分析 (EDA)、假设检验、模型构建、诊断、多重校正和报告生成。

# Print the full protocol to give to your AI agent
r_best_practices_prompt()

你也可以直接告诉代理运行 ClaudeR::r_best_practices_prompt() ,它会自动读取协议。

多智能体协调协议#

当两个或多个智能体共享同一个 RStudio 会话时,它们需要一种方法来分配工作,避免相互干扰。多智能体协议通过结构化的工作流程来解决这个问题:智能体通过读取会话日志进行签到,第一个智能体创建任务计划,智能体在开始任务前认领任务,并在任务完成后相互检查彼此的工作。

# Print the full protocol to give to your AI agents
multi_agent_prompt()

你也可以直接告诉代理运行 ClaudeR::multi_agent_prompt() ,它们会自行读取协议。

人工智能驱动的数据标注#

ClaudeR 包含一个专为使用 AI 代理标注 CSV 数据集而设计的标注工作流程。该代理使用两个专用的 MCP 工具逐行处理数据集,无需在代理端编写任何代码。

CSV 格式: 在文件中添加一个 _schema 列,并在第一行使用简单的类型语法定义注释字段:

text,label,confidence,_schema
"Some text","","","label:choice[positive,negative,neutral];confidence:float[0,1]"
"More text","","",""

支持的类型: choice[a,b,c] 、 float[min,max] 、 int[min,max] 、 bool 、 text

运行标注会话:

# Print the full protocol to give to your agent
data_annotation_prompt()

或者,您可以指示代理运行 ClaudeR::data_annotation_prompt() ,它将自动读取协议。然后,代理调用 load_annotation_data 开始 annotate ,并对每一行数据进行标记。原始文件不会被修改,会话中断后会自动恢复。

有两种标注模式可供选择: 默认的 load_annotation_data + annotate 流程在代理的现有对话中运行,上下文信息会逐行累积,这有助于保持一致性,但可能会在长数据集上引入锚定效应。要实现完全的行隔离,请改用 run_annotation_job :它会为每一行生成一个新的 claude 、 codex 、 gemini 或 qwen 进程,或者将每一行发送到本地 ollama 服务器,因此每次标注都不会受到先前行的影响。

模式 1:完整上下文(交互式):

You are annotating a dataset. Your only job is to call annotation tools. Do not write code or use any other tools.

Step 1: Call load_annotation_data with:
- csv_path: /path/to/your/file.csv

Step 2: For each row displayed, call annotate with the fields defined in the schema.

Step 3: After each annotate call, the next row loads automatically. Keep annotating until you see "Annotation complete."

If you get a validation error, read it carefully and call annotate again with corrected values.

模式 2:独立上下文(每行一个子进程):

You are annotating a dataset.

Step 1: Call run_annotation_job with:
- csv_path: /path/to/your/file.csv
- tool: claude            # or "codex" / "gemini" / "qwen" / "ollama"
- model: qwen2.5          # ollama only: any model tag you have pulled
- reasoning_effort: high  # codex only: low | medium | high
- ollama_base_url: http://localhost:11434  # ollama only, defaults shown

Step 2: Once you have the job ID, periodically call get_annotation_job_status with that ID to check progress.

To stop early, call cancel_annotation_job with the job ID. Rows already annotated are preserved.

That's it. The annotation runs automatically in the background. Do not call any other tools unless checking status.

工作原理#

ClaudeR 使用 模型上下文协议 (MCP) 在 AI 助手和您的 RStudio 环境之间建立双向连接。MCP 是 Anthropic 开发的开放协议,它允许 AI 安全地与本地工具和数据进行交互。

工作流程如下:

  1. Python MCP 服务器充当桥梁。
  2. AI 向 MCP 服务器发送代码执行请求。
  3. 服务器将请求转发给在 RStudio 中运行的 R 插件。
  4. 代码在您的 R 会话中执行,并将结果发送回 AI。

该架构确保 AI 只能通过定义良好的接口执行已批准的操作,从而使您完全掌控您的 R 环境。

CLI 集成#

ClaudeR 支持命令行界面 (CLI) 工具: Claude Code CLI 、 OpenAI Codex CLI 、 Qwen Code CLI 和 Google Gemini CLI 。这对于喜欢基于终端的工作流程的开发者来说非常理想,他们可以直接从命令行与 AI 助手交互,同时保持与 RStudio 会话的实时连接。

MCP 配置命令速查#

以下是在各 AI 工具中注册 clauder-mcp(即 ClaudeR MCP 桥接组件)的原始命令,使用 uvx 零配置运行:

Claude Code:

claude mcp add --transport stdio --scope user r-studio -- uvx clauder-mcp

Codex CLI:

codex mcp add r-studio -- uvx clauder-mcp

Qwen Code CLI:

qwen mcp add --scope user --transport stdio r-studio uvx clauder-mcp

Gemini CLI(添加到 ~/.gemini/settings.json):

{
  "mcpServers": {
    "r-studio": {
      "command": "uvx",
      "args": ["clauder-mcp"]
    }
  }
}

Claude Desktop / Cursor(添加到 MCP 配置文件):

{
  "mcpServers": {
    "r-studio": {
      "command": "uvx",
      "args": ["clauder-mcp"]
    }
  }
}

与使用 install_cli() 辅助函数相比,上述命令跳过 R 包安装步骤直接注册 MCP 服务器,适合已安装 ClaudeR 后的快速配置或手动排查连接问题。

安全模型#

ClaudeR 是一款 受监督的强大工具 。该代理会在您的 RStudio 实时会话中执行 R 代码,该会话也包含您的数据和变量。您应该像运行同事的代码一样,仔细检查它的运行结果。

ClaudeR 阻止了什么#

  • 系统命令 : system() 、 system2() 、 shell() 及相关调用被阻止,以防止代理程序访问 R 外部。
  • 文件删除 :禁止 unlink() 、 file.remove() 以及包含 rm 的 shell 命令。
  • 错误反馈 :阻塞的操作会返回清晰的错误消息,解释原因。

ClaudeR 不限制的内容#

代理程序仍然可以读取文件、安装软件包、创建/覆盖环境中的对象以及消耗计算资源。这些对于代理程序的正常运行至关重要,但也意味着您应该:

  • 使用日志记录 (默认启用),以便您可以完整记录代理执行的每一行代码以及运行该代码的代理。
  • 在项目目录中进行操作 ,以限制代理可以看到的内容。
  • 信任之前先审核 :特别是对于 Reviewer Zero 审核,应将输出结果视为草稿审核,需要进行验证。

这些限制仅适用于人工智能执行的代码。您手动执行的 R 代码不受影响。

安装#

步骤 1:从 GitHub 安装 ClaudeR#

在 RStudio 控制台中运行以下命令:

if (!require("devtools")) install.packages("devtools")
devtools::install_github("IMNMV/ClaudeR")

步骤 2:运行正确的安装程序#

选择符合您工作流程的选项。

选项 A:适用于桌面应用程序(Claude Desktop / Cursor)

此函数会自动配置桌面应用程序的 MCP 配置文件。默认情况下,它使用 uvx 运行 clauder-mcp PyPI 包,该包会自动处理所有 Python 依赖项。

# Load the package
library(ClaudeR)

# Run the installer for Claude Desktop
install_clauder()

# Optional: For Cursor users
# install_clauder(for_cursor = TRUE)

对于无法使用 uvx 用户(例如受限环境),请回退到传统的 Python 路径方法:

library(ClaudeR)
install_clauder(use_uvx = FALSE, python_path = "/path/to/your/python")

选项 B:适用于 CLI 工具(Claude Code / Codex / Qwen / Gemini)

这个非交互式函数会生成 CLI 工具所需的精确命令或 JSON 配置。

library(ClaudeR)

# For Claude Code CLI
install_cli(tools = "claude")

# For OpenAI Codex CLI
install_cli(tools = "codex")

# For Qwen Code CLI (requires \`npm install -g @qwen-code/qwen-code\`)
install_cli(tools = "qwen")

# For Google Gemini CLI
install_cli(tools = "gemini")

对于无法使用 uvx 的用户,请回退到传统的 Python 路径方法:

install_cli(tools = "claude", use_uvx = FALSE, python_path = "/path/to/my/python")

运行该函数后,您必须 手动应用配置 :

  • 对于 Claude / Codex / Qwen :复制 R 控制台中打印的命令,并在终端中运行它。
  • 对于 Gemini :复制生成的 JSON 并手动将其添加到您的 gemini.json 设置文件中。

设置完成后, 请退出并重新启动 所有正在运行的桌面应用程序或终端会话,以便加载新设置。

注意 :如果您升级了 R 版本,请重新运行 install_cli() 或 install_clauder() 以更新 MCP 服务器路径。CLI 安装程序会在添加新注册信息之前自动删除过期的注册信息。

用法#

第一部分:在 RStudio 中#

对于 所有 工作流程,您必须首先从 RStudio 启动 ClaudeR 服务器。

library(ClaudeR)
claudeAddin()

ClaudeR 插件将出现在 RStudio 查看器窗格中。点击 "启动服务器" 。在使用您喜欢的工具时,请保持此窗口处于活动状态。

ClaudeR Addin Interface

第二部分:在你的 AI 工具中#

  • 对于桌面应用程序 :打开 Claude 桌面应用程序或光标,然后开始您的会话。
  • 对于 CLI 工具 :打开终端并使用 claude 、 codex 、 qwen 或 gemini 命令开始与您的 AI 助手进行交互。

注意:您可以通过单击 RStudio 控制台中的停止按钮来重新获得控制台/活动文档的控制权。这将关闭 Shiny UI,但 MCP 服务器会在后台继续运行,您的 AI 代理也会保持连接。重新运行 claudeAddin() 即可恢复查看器窗格,并保持相同的服务器状态(端口、会话名称、执行计数)。要完全停止服务器,请在关闭前单击 UI 中的 "停止服务器" 。

日志记录选项#

  • 将代码打印到控制台 :在 AI 运行之前,您可以在 R 控制台中查看其代码。代码前面会显示标题: ### LLM [agent-id] executing the following code ### 。
  • 将代码记录到文件 :将所有执行的代码保存到日志文件中。每个条目都会标记执行该条目的代理 ID,因此您可以追踪哪个 AI 代理执行了哪些操作。
  • 自定义日志路径 :指定日志文件的自定义位置。
  • 描述性文件名 :日志文件命名为 clauder_<session>_<port>_<timestamp>.R (例如 clauder_default_8787_20260301_143022.R ),这样您就可以一目了然地知道哪个会话生成了哪个日志。
  • 可复现性头部信息 :每条日志都以一个头部信息开头,其中包含日期、工作目录和完整的 sessionInfo() 输出(R 版本、平台、已加载/附加的包)。这使得日志具有自文档性,便于复现。
  • 导出干净脚本 :在日志面板中单击"导出干净脚本"以生成一个可运行的 .R 文件,该文件已去除所有时间戳和日志头。错误块将保留为注释,以便您可以查看出错原因。也可以使用 export_log_as_script() 从控制台调用。

每次单击 "启动服务器" 时,都会创建一个新的日志文件。代理执行的所有代码都会追加到该文件中,直到您停止并再次启动服务器为止。

交互示例#

  • "我的环境中有一个名为 data 的数据集。请对其执行探索性数据分析。"
  • "加载 mtcars 数据集,并创建 mpg 与 hp 的散点图,并添加趋势线。"
  • "根据 wt 和 hp 拟合线性模型来预测 mpg 。"
  • "为 iris 数据集生成相关矩阵并将其可视化。"
  • 我有一个 QMD 文件正在运行。请制作一个关于梯度下降的精美四开本演示文稿。听众都是技术人员。请确保演示文稿流畅美观。请将演示文稿保存在/Users/nyk/QuartoDocs/文件夹中。

如果你能用 R 语言做到,你的 AI 助手也能做到。

重要提示#

  • 会话持久性 :AI 创建的变量、数据和函数将保留在您的 R 会话中。
  • 代码可见性 :默认情况下,AI 的代码会打印到您的控制台。
  • 端口配置 :默认端口为 8787 ,但您可以根据需要更改它。方法一:在 Shiny 插件界面的 Advanced 设置中手动改端口;方法二:用 claude_rstudio_addin(port = 21015) 启动指定端口。
  • 软件包安装 :AI 可以安装软件包。请使用清晰的提示来引导其行为。如果您注重可复现性,请在 renv 项目中进行操作: renv 会劫持当前会话的 install.packages() 函数,因此代理的安装会进入您的项目库,而不是全局库。
  • 停止连接 :关闭 Shiny 插件(控制台中的"停止"按钮或"完成")并 不会 终止 MCP 服务器。重新运行 claudeAddin() 函数即可恢复 Viewer 并保持相同的服务器状态。要彻底停止桥接,请在关闭插件前,在插件界面中点击 "停止服务器" 。

故障排除#

  • 连接问题 :
    • 请确保您的 AI 工具配置正确。
      • 请检查 config 或 CLI 命令中的 Python 路径。
      • 请确保服务器在插件中正在运行。
      • 如果端口正在使用,请重启 RStudio。
  • Python 依赖问题 :
    • could not find function install_clauder :请重新启动 R 会话( Session -> Restart R )并重试。
      • MCP 服务器启动失败 :如果使用 uvx ,请确保已安装 uv ( curl -LsSf https://astral.sh/uv/install.sh | sh )。如果使用旧方法,这通常意味着检测到了错误的 Python 环境。请使用正确的 python_path 重新运行安装程序,或将 use_uvx = TRUE 。
  • 人工智能看不到结果 :
    • 请确保加载项窗口已打开且服务器正在运行。
  • 图表未显示 :
    • 指示 AI 将绘图对象包装在 print() 中(例如, print(my_plot) )。
      • 告诉 AI 使用 execute_r_with_plot 函数。
  • 长时间运行的代码超时 :
    • 要求 AI 对执行时间超过 25 秒的代码使用 execute_r_async 。
      • AI 将使用 get_async_result 自动轮询结果。
      • 异步作业通过 callr 在单独的 R 进程中运行, 无法 访问主会话的环境。AI 必须编写独立的代码,使用 saveRDS() 传入数据并输出结果,然后在作业完成后将结果加载回主会话。
  • 服务器重启问题 :
    • 如果在重启服务器后看到"地址已被占用"的错误提示,这是用户界面错误,服务器仍然处于运行状态。如果遇到连接问题,请在查看器窗格中切换端口号或重启 RStudio。
      • 如果 AI 仍然无法连接,请点击"高级"设置下的 "强制释放端口" 。这将强制终止占用端口的进程,以便您可以重新开始。
  • R 升级后 MCP 路径失效 :
    • 如果在升级 R 后工具停止工作,请重新运行 install_cli() 或 install_clauder() 以更新脚本路径。

局限性#

  • 每个 R 会话一次只能连接到一个 Claude Desktop/Cursor 应用程序。但是,多个 CLI 代理(Claude Code、Codex、Qwen Code、Gemini)可以与一个 Desktop 应用程序共享同一个会话。要隔离不同的代理,请使用不同的会话名称和端口运行单独的 RStudio 窗口。
  • R 语言是单线程的,但异步作业会通过 callr 在单独的进程中运行,因此主会话保持响应。默认情况下,后台进程不共享主会话的环境。可以使用 execute_r_async 的 inputs / outputs 参数自动编组输入和输出数据,或者编写独立的代码手动使用 saveRDS() / readRDS() 函数。

执照#

本项目采用 MIT 许可证 。详情请参阅 LICENSE 文件。

贡献#

欢迎贡献代码!请随时提交 Pull Request。