R 與 C++ 整合

開發環境

範例程式 (解方程式):

#include using namespace Rcpp; // [[Rcpp::export]] // R 矩陣: NumericMatrix // R 向量: NumericVector NumericVector solveEquation(NumericMatrix A, NumericVector b) { Rcpp::NumericVector vAns; // 使用當前 R 環境變數 Environment env = Environment::global_env(); // 使用 R 套件 Rcpp::Environment base("package:base"); // 映射 R 套件函式 Function solve = base["solve"]; vAns = solve(A, b); return vAns; }

library(NewCourse) p <- solveEquation(matrix(c(3,2,1,-4), byrow=T, nrow=2, ncol=2), c(7,3)) p