add-rcpp-integration

Solid

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.

AI & Automation 26 stars 4 forks Updated today MIT

Install

View on GitHub

Quality Score: 81/100

Stars 20%
48
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# 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...

Details

Author
pjt222
Repository
pjt222/agent-almanac
Created
1 years ago
Last Updated
today
Language
R
License
MIT

Similar Skills

Semantically similar based on skill content — not just same category