Zurück zu Fähigkeiten

add-rcpp-integration

pjt222
Aktualisiert 2 days ago
5 Ansichten
17
2
17
Auf GitHub ansehen
Metatesting

Über

Diese Fähigkeit integriert Rcpp oder RcppArmadillo in ein R-Paket, um hochperformanten C++-Code für Leistungsengpässe zu implementieren. Sie übernimmt den vollständigen Setup, das Schreiben von C++-Funktionen, das Generieren von RcppExports und das Testen des kompilierten Codes. Verwenden Sie sie, wenn das Profiling bestätigt, dass eine R-Funktion zu langsam ist, Sie auf C/C++-Bibliotheken zugreifen müssen oder Algorithmen wie Schleifen und lineare Algebra von Kompilierung profitieren würden.

Schnellinstallation

Claude Code

Empfohlen
Primär
npx skills add pjt222/agent-almanac -a claude-code
Plugin-BefehlAlternativ
/plugin add https://github.com/pjt222/agent-almanac
Git CloneAlternativ
git clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/add-rcpp-integration

Kopieren Sie diesen Befehl und fügen Sie ihn in Claude Code ein, um diese Fähigkeit zu installieren

Dokumentation

Add Rcpp Integration

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

When Use

  • R function too slow and profiling confirms bottleneck
  • Need to interface with existing C/C++ libraries
  • Implementing algorithms 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)

Steps

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. R/packagename-package.R contains @useDynLib directive.

If fail: usethis::use_rcpp() fails? Manually create src/, add LinkingTo: Rcpp and Imports: Rcpp to DESCRIPTION. Add #' @useDynLib packagename, .registration = TRUE and #' @importFrom Rcpp sourceCpp to 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 file uses #include <Rcpp.h> (or <RcppArmadillo.h> for Armadillo). Export annotation on its own line directly above function signature. 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 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 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. Confirm C++ function produces identical results to R equivalent. Handles edge cases (empty vectors, NA values) correctly.

If fail: Tests fail on NA handling? Add explicit NA checks in C++ code using NumericVector::is_na(). Tests fail on empty input? Add guard clause for zero-length vectors at top of 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. cleanup script removes compiled objects. Both files exist at package root level.

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

Step 7: Update .Rbuildignore

Ensure compiled artifacts handled:

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

Got: .Rbuildignore patterns prevent compiled object files from being included in package tarball. Preserves 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, .dll files.

Checks

  • 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 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, Linux.
  • Missing @useDynLib: Package-level doc must include @useDynLib packagename, .registration = TRUE

See Also

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

pjt222/agent-almanac
Pfad: i18n/caveman/skills/add-rcpp-integration
0
agentsagentskillsai-assisted-developmentclaude-codeskillsteams

Verwandte Skills

content-collections

Meta

Diese Skill bietet eine produktionsgetestete Einrichtung für Content Collections – ein TypeScript-first-Tool, das Markdown/MDX-Dateien in typsichere Datensammlungen mit Zod-Validierung umwandelt. Verwenden Sie ihn beim Erstellen von Blogs, Dokumentationsseiten oder inhaltsstarken Vite + React-Anwendungen, um Typsicherheit und automatische Inhaltsvalidierung zu gewährleisten. Er behandelt alles von der Vite-Plugin-Konfiguration und MDX-Kompilierung bis hin zur Deployment-Optimierung und Schema-Validierung.

Skill ansehen

polymarket

Meta

Diese Fähigkeit ermöglicht es Entwicklern, Anwendungen mit der Polymarket-Prognosemärkte-Plattform zu erstellen, einschließlich API-Integration für Handel und Marktdaten. Sie bietet außerdem Echtzeit-Datenstreaming über WebSocket, um Live-Trades und Marktaktivitäten zu überwachen. Nutzen Sie sie zur Implementierung von Handelsstrategien oder zur Erstellung von Tools, die Live-Marktaktualisierungen verarbeiten.

Skill ansehen

creating-opencode-plugins

Meta

Diese Fähigkeit unterstützt Entwickler dabei, OpenCode-Plugins zu erstellen, die in über 25 Ereignistypen wie Befehle, Dateien und LSP-Operationen eingreifen. Sie bietet die Plugin-Struktur, Event-API-Spezifikationen und Implementierungsmuster für JavaScript/TypeScript-Module. Nutzen Sie sie, wenn Sie den Lebenszyklus des OpenCode KI-Assistenten mit benutzerdefinierter ereignisgesteuerter Logik abfangen, überwachen oder erweitern müssen.

Skill ansehen

sglang

Meta

SGLang ist ein hochperformantes LLM-Serving-Framework, das sich auf schnelle, strukturierte Generierung für JSON, Regex und agentenbasierte Workflows unter Verwendung seines RadixAttention-Prefix-Cachings spezialisiert. Es bietet deutlich schnellere Inferenz, insbesondere für Aufgaben mit wiederholten Präfixen, was es ideal für komplexe, strukturierte Ausgaben und Mehrfachdialoge macht. Wählen Sie SGLang gegenüber Alternativen wie vLLM, wenn Sie constrained decoding benötigen oder Anwendungen mit umfangreicher Präfix-Weitergabe entwickeln.

Skill ansehen