brainstorming
brainstorming
You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).
add-rcpp-integrationAdd 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, recur…
Independent GitHub snapshot · Automated classification · Original documentation governs
入 C++ 於 R 包以速關行。
usethis::use_rcpp()
此:
src/Rcpp 於 LinkingTo 與 ImportsR/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: Rcpp 與 Imports: Rcpp 於 DESCRIPTION,入 #' @useDynLib packagename, .registration = TRUE 與 #' @importFrom Rcpp sourceCpp 於包級文。
建 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 有效型。
Rcpp::compileAttributes()
devtools::document()
得:R/RcppExports.R 與 src/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 規。
確編產妥處:
^src/.*\.o$
^src/.*\.so$
^src/.*\.dll$
得:.Rbuildignore 紋阻編對檔入包包,存源檔與 Makevars。
敗:行 devtools::check() 察 NOTE 關 src/ 中意外檔。調紋唯排 .o、.so、.dll。
devtools::load_all() 編無警R CMD check 過無編警compileAttributes():改 C++ 後須重生 RcppExportsdouble 非 intdeleteRcpp::NumericVector::is_na() 檢@useDynLib:包級文須含 @useDynLib packagename, .registration = TRUEcreate-r-package — 增 Rcpp 前包設write-testthat-tests — 測編函setup-github-actions-ci — CI 須有 C++ 工submit-to-cran — 編包需 CRAN 加察