← ClaudeAtlas

add-rcpp-integrationlisted

Add Rcpp or RcppArmadillo integration to an R package for high-performance C++ code. Covers setup, writing C++ functions, RcppExports generation, testing compiled code, and debugging. Use when an R function is too slow and profiling confirms a bottleneck, when you need to interface with existing C/C++ libraries, or when implementing algorithms (loops, recursion, linear algebra) that benefit from compiled code.
pjt222/agent-almanac · ★ 33 · AI & Automation · score 81
Install: claude install-skill pjt222/agent-almanac
# 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 ```r 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: ```r 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`: ```cpp #include <Rcpp.h> using namespace Rcpp; //' Compute c