关于
This skill adds Rcpp or RcppArmadillo integration to an R package to enable high-performance C++ code. It covers the full workflow from setup and writing C++ functions to generating RcppExports, testing, and debugging. Use it when R functions are too slow, you need to interface with C/C++ libraries, or algorithms like loops and linear algebra would benefit from compiled code.
快速安装
Claude Code
推荐npx skills add pjt222/agent-almanac -a claude-code/plugin add https://github.com/pjt222/agent-almanacgit clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/add-rcpp-integration在 Claude Code 中复制并粘贴此命令以安装该技能
技能文档
納 Rcpp 整合
於 R 包以 Rcpp 納 C++ 碼,用於性能要之操作。
用時
- R 函過緩而剖析確認瓶頸乃用
- 需連既有 C/C++ 庫乃用
- 實益於編譯之算法(環、遞歸)乃用
- 增 RcppArmadillo 以行線性代數乃用
入
- 必要:既存之 R 包
- 必要:將以 C++ 代或增之 R 函
- 可選:將連之外 C++ 庫
- 可選:用 RcppArmadillo 乎(默:純 Rcpp)
法
第一步:設 Rcpp 基
usethis::use_rcpp()
此舉:
- 建
src/目 - 於 DESCRIPTION 之 LinkingTo 與 Imports 添
Rcpp - 建
R/packagename-package.R,含@useDynLib與@importFrom Rcpp sourceCpp - 更
.gitignore以避編譯檔
用 RcppArmadillo:
usethis::use_rcpp_armadillo()
得: src/ 目已建,DESCRIPTION 已更 LinkingTo 與 Imports 之 Rcpp,R/packagename-package.R 含 @useDynLib 指令。
敗則: 若 usethis::use_rcpp() 敗,手建 src/,於 DESCRIPTION 添 LinkingTo: Rcpp 與 Imports: Rcpp,於包級文檔添 #' @useDynLib packagename, .registration = TRUE 與 #' @importFrom Rcpp sourceCpp。
第二步:書 C++ 函
建 src/my_function.cpp:
#include <Rcpp.h>
using namespace Rcpp;
//' Compute cumulative sum efficiently
//'
//' @param x A numeric vector
//' @return A numeric vector of cumulative sums
//' @export
// [[Rcpp::export]]
NumericVector cumsum_cpp(NumericVector x) {
int n = x.size();
NumericVector out(n);
out[0] = x[0];
for (int i = 1; i < n; i++) {
out[i] = out[i - 1] + x[i];
}
return out;
}
用 RcppArmadillo:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
//' Matrix multiplication using Armadillo
//'
//' @param A A numeric matrix
//' @param B A numeric matrix
//' @return The matrix product A * B
//' @export
// [[Rcpp::export]]
arma::mat mat_mult(const arma::mat& A, const arma::mat& B) {
return A * B;
}
得: C++ 源檔存於 src/my_function.cpp,有 // [[Rcpp::export]] 注及 //' 之 roxygen 注。
敗則: 驗用 #include <Rcpp.h>(Armadillo 則 <RcppArmadillo.h>),導出注自成一行居函簽上,返型合於 Rcpp 型。
第三步:生 RcppExports
Rcpp::compileAttributes()
devtools::document()
得: R/RcppExports.R 與 src/RcppExports.cpp 自生。
敗則: 察 C++ 之語法訛。確 // [[Rcpp::export]] 置各導出函之上。
第四步:驗編譯
devtools::load_all()
得: 包編而載無訛。
敗則: 察編譯器之出。常患:
- 系統頭缺:裝開發庫
- 語法訛:編譯器指其行
- RcppArmadillo 缺
Rcpp::depends注
第五步:書編碼之試
test_that("cumsum_cpp matches base R", {
x <- c(1, 2, 3, 4, 5)
expect_equal(cumsum_cpp(x), cumsum(x))
})
test_that("cumsum_cpp handles edge cases", {
expect_equal(cumsum_cpp(numeric(0)), numeric(0))
expect_equal(cumsum_cpp(c(NA_real_, 1)), c(NA_real_, NA_real_))
})
得: 試皆過,確 C++ 函與 R 等,處邊例(空量、NA)正。
敗則: NA 試敗則於 C++ 中以 NumericVector::is_na() 明察。空入試敗則函首加長零之守。
第六步:添清理之本
建 src/Makevars:
PKG_CXXFLAGS = -O2
建 cleanup 於包根(為 CRAN):
#!/bin/sh
rm -f src/*.o src/*.so src/*.dll
授可執:chmod +x cleanup
得: src/Makevars 設編譯旗,cleanup 本除編譯物。二檔皆於包根。
敗則: 驗 cleanup 可執(chmod +x cleanup),Makevars 用 tab(非空格)縮以寫 Makefile 之則。
第七步:更 .Rbuildignore
確編譯物已處:
^src/.*\.o$
^src/.*\.so$
^src/.*\.dll$
得: .Rbuildignore 之式避編譯物入包焰,而存源與 Makevars。
敗則: 行 devtools::check() 察 src/ 未期之檔之 NOTE。調 .Rbuildignore 只排 .o、.so、.dll。
驗
-
devtools::load_all()編而無警 - 編函生正果
- 邊例(NA、空、大入)之試皆過
-
R CMD check過無編譯之警 - RcppExports 檔已生並提交
- 以基準確性能之進
陷
- 遺
compileAttributes():改 C++ 後必再生 RcppExports - 整溢:大數用
double非int - 記憶之治:Rcpp 自治其型之記憶;勿手
delete - NA 之處:C++ 不識 R 之 NA。以
Rcpp::NumericVector::is_na()察 - 平臺可攜:避平臺特之 C++ 特性。試於 Windows、macOS、Linux
@useDynLib闕:包級文檔必含@useDynLib packagename, .registration = TRUE
參
create-r-package— 增 Rcpp 前之包設write-testthat-tests— 試編譯之函setup-github-actions-ci— CI 須有 C++ 之具submit-to-cran— 編譯包需額外 CRAN 之查
GitHub 仓库
Frequently asked questions
What is the add-rcpp-integration skill?
add-rcpp-integration is a Claude Skill by pjt222. Skills package instructions and resources that Claude loads on demand, so Claude can perform add-rcpp-integration-related tasks without extra prompting.
How do I install add-rcpp-integration?
Use the install commands on this page: add add-rcpp-integration to Claude Code as a plugin, or clone its repository into your skills directory, then restart Claude so it picks up the skill.
What category does add-rcpp-integration belong to?
add-rcpp-integration is in the Meta category, tagged testing.
Is add-rcpp-integration free to use?
Yes. add-rcpp-integration is listed on AIMCP and free to install. It runs inside Claude, so no separate service account is required to use the skill itself.
相关推荐技能
Content Collections 是一个 TypeScript 优先的构建工具,可将本地 Markdown/MDX 文件转换为类型安全的数据集合。它专为构建博客、文档站和内容密集型 Vite+React 应用而设计,提供基于 Zod 的自动模式验证。该工具涵盖从 Vite 插件配置、MDX 编译到生产环境部署的完整工作流。
这个Claude Skill为开发者提供完整的Polymarket预测市场开发支持,涵盖API调用、交易执行和市场数据分析。关键特性包括实时WebSocket数据流,可监控实时交易、订单和市场动态。开发者可用它构建预测市场应用、实施交易策略并集成实时市场预测功能。
该Skill帮助开发者创建OpenCode插件,用于接入命令、文件、LSP等25+种事件。它提供了插件结构、事件API规范和JavaScript/TypeScript实现模式,适合需要拦截操作、扩展功能或自定义事件处理的场景。开发者可通过它快速构建响应式模块来增强OpenCode AI助手的能力。
SGLang是一个专为LLM设计的高性能推理框架,特别适用于需要结构化输出的场景。它通过RadixAttention前缀缓存技术,在处理JSON、正则表达式、工具调用等具有重复前缀的复杂工作流时,能实现极速生成。如果你正在构建智能体或多轮对话系统,并追求远超vLLM的推理性能,SGLang是理想选择。
