Back to Skills

add-rcpp-integration

pjt222
Updated 6 days ago
15 views
17
2
17
View on GitHub
Metatesting

About

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.

Quick Install

Claude Code

Recommended
Primary
npx skills add pjt222/agent-almanac -a claude-code
Plugin CommandAlternative
/plugin add https://github.com/pjt222/agent-almanac
Git CloneAlternative
git clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/add-rcpp-integration

Copy and paste this command in Claude Code to install this skill

Documentation

接 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 Repository

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

Related Skills

content-collections

Meta

This skill provides a production-tested setup for Content Collections, a TypeScript-first tool that transforms Markdown/MDX files into type-safe data collections with Zod validation. Use it when building blogs, documentation sites, or content-heavy Vite + React applications to ensure type safety and automatic content validation. It covers everything from Vite plugin configuration and MDX compilation to deployment optimization and schema validation.

View skill

polymarket

Meta

This skill enables developers to build applications with the Polymarket prediction markets platform, including API integration for trading and market data. It also provides real-time data streaming via WebSocket to monitor live trades and market activity. Use it for implementing trading strategies or creating tools that process live market updates.

View skill

creating-opencode-plugins

Meta

This skill helps developers create OpenCode plugins that hook into 25+ event types like commands, files, and LSP operations. It provides the plugin structure, event API specifications, and implementation patterns for JavaScript/TypeScript modules. Use it when you need to intercept, monitor, or extend the OpenCode AI assistant's lifecycle with custom event-driven logic.

View skill

sglang

Meta

SGLang is a high-performance LLM serving framework that specializes in fast, structured generation for JSON, regex, and agentic workflows using its RadixAttention prefix caching. It delivers significantly faster inference, especially for tasks with repeated prefixes, making it ideal for complex, structured outputs and multi-turn conversations. Choose SGLang over alternatives like vLLM when you need constrained decoding or are building applications with extensive prefix sharing.

View skill