Documente Academic
Documente Profesional
Documente Cultură
## Add R code to pkg R/ directory. Call C++ // Place the export tag right above function declaration.
function. Do type-checking in R.
// [[Rcpp::export]]
myfunR = function(Rx, Ry) { double muRcpp(NumericVector x){
ret = .Call("myCfun", Rx, Ry, int n = x.size(); // Size of vector
package="myPackage") double sum = 0; // Sum value
return(ret)
} // For loop, note cpp index shift to 0
for(int i = 0; i < n; i++){
// Add C++ code to pkg src/ directory. // Shorthand for sum = sum + x[i]
using namespace Rcpp; sum += x[i];
// Define function as extern with RcppExport }
RcppExport SEXP myCfun( SEXP x, SEXP y) {
// If R/C++ types match, use pointer to x. Pointer is return sum/n; // Obtain and return the Mean
faster, but changes to xx propagate to R ( xx -> x == Rx). }
NumericVector xx(x);
// clone is slower and uses extra memory. Safe, R-like. // Place dependent functions above call or
NumericVector yy(clone(y)); // declare the function definition with:
xx[0] = yy[0] = -1.5; double muRcpp(NumericVector x);
int zz = xx[0];
// use wrap() to return non-SEXP objects, e.g: // [[Rcpp::export]]
// return(wrap(zz)); double varRcpp(NumericVector x, bool bias =
// Build and return a list true){
List ret; ret["x"] = xx; ret["y"] = yy; // Calculate the mean using C++ function
return(ret); double mean = muRcpp(x);
} double sum = 0;
int n = x.size();
## From shell, above package directory
R CMD check myPackage ## Optional for(int i = 0; i < n; i++){
R CMD INSTALL myPackage sum += pow(x[i] - mean, 2.0); // Square
}
## In R:
require(myPackage) return sum/(n-bias); // Return variance
aa = 1.5; bb = 1.5; cc = myfunR(aa, bb) }
aa == bb ## FALSE, C++ modifies aa
aa = 1:2; bb = 1:2; cc = myfunR(aa, bb) ## In R:
identical(aa, bb) require(Rcpp)
## TRUE, R/C++ types don’t match sourceCpp("path/to/file/Rcpp_example.cpp")
x = 1:5;
all.equal(muRcpp(x), mean(x));
all.equal(var(x),varRcpp(x))
## TRUE
Rcpp Extensions
STL interface // Enable C++11
// sum a vector from beginning to end // [[Rcpp::plugins(cpp11)]]
double s = std::accumulate(x.begin(),
x.end(), 0.0); // Enable OpenMP (excludes macOS)
// prod of elements from beginning to end // [[Rcpp::plugins(openmp)]]
int p = std::accumulate(vec.begin(),
vec.end(), 1, std::multiplies<int>()); // Use the RcppArmadillo package
// inner_product to compute sum of squares // Requires different header file from Rcpp.h
double s2 = std::inner_product(res.begin(), #include <RcppArmadillo.h>
res.end(), res.begin(), 0.0); // [[Rcpp::depends(RcppArmadillo)]]
2
Rcpp sugar Random functions
NumericVector x = NumericVector::create( // Set seed
-2.0, -1.0, 0.0, 1.0, 2.0 ); RNGScope scope;
IntegerVector y = IntegerVector::create(
-2, -1, 0, 1, 2 ); // For details see Section 6.7.1--Distribution functions
of the ‘Writing R Extensions’ manual. In some cases (e.g.
NumericVector xx = abs( x ); rnorm), distribution-specific arguments can be omitted;
IntegerVector yy = abs( y ); when in doubt, specify all dist-specific arguments. The use of
doubles rather than integers for dist-specific arguments is
bool b = all( x < 3.0 ).is_true() ; recommended. Unless explicitly specified, log=FALSE.
bool b = any( y > 2 ).is_true();
// Equivalent to R calls
NumericVector xx = ceil( x ); NumericVector xx = runif(20);
NumericVector xx = ceiling( x ); NumericVector xx1 = rnorm(20);
NumericVector yy = floor( y ); NumericVector xx1 = rnorm(20, 0);
NumericVector yy = floor( y ); NumericVector xx1 = rnorm(20, 0, 1);
3
Environment Modules
// Obtain an R environment // Warning -- At present, module-based objects do not
Environment stats("package:stats"); persist across quit(save="yes")/reload cycles. To be safe,
Environment env( 2 ); // by position save results to R objects and remove module objects before
exiting R.
// Special environments
Environment::Rcpp_namespace(); // To create a module-containing package from R, use:
Environment::base_env(); Rcpp.package.skeleton("mypackage",module=TRUE)
Environment::base_namespace(); // You will need to edit the RcppModules: line of the DE-
Environment::global_env(); SCRIPTION file to match your module name (in this example,
Environment::empty_env(); from yada to mod_bar).
## In R:
x = 1:5
callFunction(x, sum)