MCP HubMCP Hub
Вернуться к навыкам

add-rcpp-integration

pjt222
Обновлено 2 days ago
7 просмотров
17
2
17
Посмотреть на GitHub
Метаtesting

О программе

Этот навык добавляет интеграцию с Rcpp или RcppArmadillo в пакет R, позволяя использовать высокопроизводительный код на C++ для узких мест, взаимодействия с библиотеками или сложных алгоритмов. Он проводит разработчиков через настройку, написание функций на C++, генерацию RcppExports и тестирование скомпилированного кода. Используйте его, когда профилирование подтверждает, что функция R слишком медленная, или вам необходимо задействовать скомпилированный код для циклов, рекурсии или линейной алгебры.

Быстрая установка

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 для установки этого навыка

Документация

Add Rcpp Integration

Integrate C++ code into an R package using Rcpp for performance-critical operations.

When to Use

  • R function is too slow and profiling confirms a bottleneck
  • Need to interface with existing C/C++ libraries
  • Implementing algorithms that benefit from compiled code (loops, recursion)
  • Adding RcppArmadillo for linear algebra operations

Inputs

  • Required: Existing R package
  • Required: R function to replace or augment with C++
  • Optional: External C++ library to interface with
  • Optional: Whether to use RcppArmadillo (default: plain Rcpp)

Procedure

Step 1: Set Up Rcpp Infrastructure

usethis::use_rcpp()

This:

  • Creates src/ directory
  • Adds Rcpp to LinkingTo and Imports in DESCRIPTION
  • Creates R/packagename-package.R with @useDynLib and @importFrom Rcpp sourceCpp
  • Updates .gitignore for compiled files

For RcppArmadillo:

usethis::use_rcpp_armadillo()

Got: src/ directory created, DESCRIPTION updated with Rcpp in LinkingTo and Imports, and R/packagename-package.R contains @useDynLib directive.

If fail: If usethis::use_rcpp() fails, manually create src/, add LinkingTo: Rcpp and Imports: Rcpp to DESCRIPTION, and add #' @useDynLib packagename, .registration = TRUE and #' @importFrom Rcpp sourceCpp to the package-level documentation file.

Step 2: Write C++ Function

Create 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;
}

For 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;
}

Got: C++ source file exists at src/my_function.cpp with valid // [[Rcpp::export]] annotation and roxygen-style //' documentation comments.

If fail: Verify the file uses #include <Rcpp.h> (or <RcppArmadillo.h> for Armadillo), that the export annotation is on its own line directly above the function signature, and that return types map to valid Rcpp types.

Step 3: Generate RcppExports

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

Got: R/RcppExports.R and src/RcppExports.cpp generated automatically.

If fail: Check C++ syntax errors. Ensure // [[Rcpp::export]] tag is present above each exported function.

Step 4: Verify Compilation

devtools::load_all()

Got: Package compiles and loads without errors.

If fail: Check compiler output for errors. Common issues:

  • Missing system headers: Install development libraries
  • Syntax errors: C++ compiler messages point to the line
  • Missing Rcpp::depends attribute for RcppArmadillo

Step 5: Write Tests for Compiled Code

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_))
})

Got: Tests pass, confirming the C++ function produces identical results to the R equivalent and handles edge cases (empty vectors, NA values) correctly.

If fail: If tests fail on NA handling, add explicit NA checks in the C++ code using NumericVector::is_na(). If tests fail on empty input, add a guard clause for zero-length vectors at the top of the function.

Step 6: Add Cleanup Script

Create src/Makevars:

PKG_CXXFLAGS = -O2

Create cleanup in package root (for CRAN):

#!/bin/sh
rm -f src/*.o src/*.so src/*.dll

Make executable: chmod +x cleanup

Got: src/Makevars sets compiler flags and cleanup script removes compiled objects. Both files exist at the package root level.

If fail: Verify cleanup has execute permissions (chmod +x cleanup) and that Makevars uses tabs (not spaces) for indentation if adding Makefile-style rules.

Step 7: Update .Rbuildignore

Ensure compiled artifacts are handled:

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

Got: .Rbuildignore patterns prevent compiled object files from being included in the package tarball, while preserving source files and Makevars.

If fail: Run devtools::check() and look for NOTEs about unexpected files in src/. Adjust .Rbuildignore patterns to exclude only .o, .so, and .dll files.

Validation

  • devtools::load_all() compiles without warnings
  • Compiled function produces correct results
  • Tests pass for edge cases (NA, empty, large inputs)
  • R CMD check passes with no compilation warnings
  • RcppExports files are generated and committed
  • Performance improvement confirmed with benchmarks

Pitfalls

  • Forgetting compileAttributes(): Must regenerate RcppExports after changing C++ files
  • Integer overflow: Use double instead of int for large numeric values
  • Memory management: Rcpp handles memory automatically for Rcpp types; don't manually delete
  • NA handling: C++ doesn't know about R's NA. Check with Rcpp::NumericVector::is_na()
  • Platform portability: Avoid platform-specific C++ features. Test on Windows, macOS, and Linux.
  • Missing @useDynLib: The package-level doc must include @useDynLib packagename, .registration = TRUE

Related Skills

  • create-r-package - package setup before adding Rcpp
  • write-testthat-tests - testing compiled functions
  • setup-github-actions-ci - CI must have C++ toolchain
  • submit-to-cran - compiled packages need extra CRAN checks

GitHub репозиторий

pjt222/agent-almanac
Путь: i18n/caveman-lite/skills/add-rcpp-integration
0
agentsagentskillsai-assisted-developmentclaude-codeskillsteams

Похожие навыки

content-collections

Мета

Этот навык предоставляет проверенную в продакшене настройку для Content Collections — TypeScript-ориентированного инструмента, который преобразует файлы Markdown/MDX в типобезопасные коллекции данных с валидацией Zod. Используйте его при создании блогов, сайтов документации или контентных приложений на Vite + React для обеспечения типобезопасности и автоматической проверки содержимого. Он охватывает всё: от настройки плагина Vite и компиляции MDX до оптимизации развертывания и валидации схем.

Просмотреть навык

polymarket

Мета

Этот навык позволяет разработчикам создавать приложения на платформе прогнозных рынков Polymarket, включая интеграцию с API для торговли и получения рыночных данных. Он также обеспечивает потоковую передачу данных в реальном времени через WebSocket для отслеживания текущих сделок и рыночной активности. Используйте его для реализации торговых стратегий или создания инструментов, обрабатывающих обновления рынка в реальном времени.

Просмотреть навык

creating-opencode-plugins

Мета

Этот навык помогает разработчикам создавать плагины OpenCode, которые подключаются к более чем 25 типам событий, таким как команды, файлы и операции LSP. Он предоставляет структуру плагина, спецификации API событий и шаблоны реализации для модулей на JavaScript/TypeScript. Используйте его, когда вам нужно перехватывать, отслеживать или расширять жизненный цикл ассистента OpenCode AI с помощью пользовательской событийно-ориентированной логики.

Просмотреть навык

sglang

Мета

SGLang — это высокопроизводительный фреймворк для обслуживания больших языковых моделей (LLM), специализирующийся на быстрой структурированной генерации JSON, regex и рабочих процессов агентов с использованием кэширования префиксов RadixAttention. Он обеспечивает значительно более высокую скорость вывода, особенно для задач с повторяющимися префиксами, что делает его идеальным для сложных структурированных результатов и многократных диалогов. Выбирайте SGLang вместо альтернатив, таких как vLLM, когда вам требуется ограниченное декодирование или вы создаете приложения с интенсивным совместным использованием префиксов.

Просмотреть навык