返回技能列表

add-rcpp-integration

pjt222
更新于 6 days ago
19 次查看
17
2
17
在 GitHub 上查看
testing

关于

This skill adds Rcpp or RcppArmadillo integration to an R package, enabling high-performance C++ code for computationally intensive tasks. It guides developers through setup, writing C++ functions, generating RcppExports, and testing compiled code. Use it when profiling reveals bottlenecks in R functions, when interfacing with existing C/C++ libraries, or when implementing algorithms like loops and linear algebra that benefit from compilation.

快速安装

Claude Code

推荐
主要方式
npx skills add pjt222/agent-almanac -a claude-code
插件命令备选方式
/plugin add https://github.com/pjt222/agent-almanac
Git 克隆备选方式
git clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/add-rcpp-integration

在 Claude Code 中复制并粘贴此命令以安装该技能

技能文档

接 Rcpp

入 C++ 於 R 包以速關行。

  • R 函慢、剖證瓶→用
  • 接既有 C/C++ 庫→用
  • 算(環、遞)益於編→用
  • 增 RcppArmadillo 為線代→用

  • :既存 R 包
  • :欲代或補之 R 函
  • :欲接之外 C++ 庫
  • :用 RcppArmadillo 否(默純 Rcpp)

一:設 Rcpp 基

usethis::use_rcpp()

此:

  • src/
  • Rcpp 於 LinkingTo 與 Imports
  • R/packagename-package.R@useDynLib@importFrom Rcpp sourceCpp
  • .gitignore 避編檔

RcppArmadillo:

usethis::use_rcpp_armadillo()

得:src/ 已建,DESCRIPTION 含 Rcpp 於 LinkingTo 與 Imports,R/packagename-package.R@useDynLib

敗:usethis::use_rcpp() 敗→手建 src/、入 LinkingTo: RcppImports: Rcpp 於 DESCRIPTION,入 #' @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]]//' 註。

敗:驗檔用 #include <Rcpp.h>(Armadillo 用 <RcppArmadillo.h>),出註於函簽前獨行,返型映 Rcpp 有效型。

三:生 RcppExports

Rcpp::compileAttributes()
devtools::document()

得:R/RcppExports.Rsrc/RcppExports.cpp 自生。

敗:察 C++ 語誤。確 // [[Rcpp::export]] 標於各出函上。

四:驗編

devtools::load_all()

得:包編、載而無誤。

敗:察編出。常症:缺系頭→裝開發庫;語誤→C++ 編訊指行;缺 Rcpp::depends 屬於 RcppArmadillo。

五:書測編

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 用製表(非空)若加 Makefile 規。

七:更 .Rbuildignore

確編產妥處:

^src/.*\.o$
^src/.*\.so$
^src/.*\.dll$

得:.Rbuildignore 紋阻編對檔入包包,存源檔與 Makevars。

敗:行 devtools::check() 察 NOTE 關 src/ 中意外檔。調紋唯排 .o.so.dll

  • devtools::load_all() 編無警
  • 編函出正果
  • 邊例(NA、空、巨)測過
  • R CMD check 過無編警
  • RcppExports 已生且提
  • 性能改以基準證

  • compileAttributes():改 C++ 後須重生 RcppExports
  • 整溢:大數用 doubleint
  • 記理:Rcpp 自理 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 仓库

pjt222/agent-almanac
路径: i18n/wenyan-ultra/skills/add-rcpp-integration
0
agentsagentskillsai-assisted-developmentclaude-codeskillsteams

相关推荐技能

content-collections

Content Collections 是一个 TypeScript 优先的构建工具,可将本地 Markdown/MDX 文件转换为类型安全的数据集合。它专为构建博客、文档站和内容密集型 Vite+React 应用而设计,提供基于 Zod 的自动模式验证。该工具涵盖从 Vite 插件配置、MDX 编译到生产环境部署的完整工作流。

查看技能

polymarket

这个Claude Skill为开发者提供完整的Polymarket预测市场开发支持,涵盖API调用、交易执行和市场数据分析。关键特性包括实时WebSocket数据流,可监控实时交易、订单和市场动态。开发者可用它构建预测市场应用、实施交易策略并集成实时市场预测功能。

查看技能

creating-opencode-plugins

该Skill帮助开发者创建OpenCode插件,用于接入命令、文件、LSP等25+种事件。它提供了插件结构、事件API规范和JavaScript/TypeScript实现模式,适合需要拦截操作、扩展功能或自定义事件处理的场景。开发者可通过它快速构建响应式模块来增强OpenCode AI助手的能力。

查看技能

sglang

SGLang是一个专为LLM设计的高性能推理框架,特别适用于需要结构化输出的场景。它通过RadixAttention前缀缓存技术,在处理JSON、正则表达式、工具调用等具有重复前缀的复杂工作流时,能实现极速生成。如果你正在构建智能体或多轮对话系统,并追求远超vLLM的推理性能,SGLang是理想选择。

查看技能