UP | HOME

Inline C++ Example

Author: Mitch Richling
Updated: 2022-06-04 16:17:46

Copyright 2020-2021 Mitch Richling. All rights reserved.

Table of Contents

1. Metadata

The home for this HTML file is: https://richmit.github.io/ex-R/cppInR.html

Files related to this document may be found on github: https://github.com/richmit/ex-R

Directory contents:

src - The org-mode file that generated this HTML document
docs - This html document
data - Data files
tangled - Tangled R code from this document

2. Load the library

library(Rcpp)

3. External C++ Code

We can put our code in an external file, and tell R where to find it.

3.1. The External Code File

In the code below we define two functions. The first concatinates two R vecotrs, and the second computes the sum of an R vector. We place the code into a file called cppInR.cpp. Note the export direcives in the comments!

///////////////////////////////////////////////////////////
#include <Rcpp.h>

///////////////////////////////////////////////////////////
using namespace Rcpp;

///////////////////////////////////////////////////////////
// [[Rcpp::export]]
NumericVector cppExtCat(NumericVector a, NumericVector b) {
  Rcpp::NumericVector c(a.size() + b.size());
  int j=0;
  for (int i=0; i<a.size(); i++)
    c[j++] += a[i];
  for (int i=0; i<b.size(); i++)
    c[j++] += b[i];
  return c;
}

///////////////////////////////////////////////////////////
// [[Rcpp::export]]
double cppExtSum(NumericVector x) {
  double total = 0;
  for(int i=0; i<x.size(); ++i) {
    total += x[i];
  }
  return total;
}

///////////////////////////////////////////////////////////
/*** R
print('HI -- I am some code in cppRcppOutOfLine.cpp')
*/

3.2. Telling R about our external code

We must tell R where to find our enxternal code. We do that with the sourceCpp function – it will compile the code and create R wrappers to call the functions. Note that little bit of R code in the comment at the bottom of cppInR.cpp is exicuted when we run sourceCpp!

Rcpp::sourceCpp('cppInR.cpp')
print('HI -- I am some code in cppRcppOutOfLine.cpp')
[1] "HI -- I am some code in cppRcppOutOfLine.cpp"

4. Inline C++ Code

We define the same two functions, but we put them directly in our R code as strings. Instead of using sourceCpp, we use cppFunction. cppFunction takes care of several things for us:

  • Define the function
  • Compile the C++ code
  • Create an R wrapper for a C++ function
Rcpp::cppFunction('NumericVector cppIntCat(NumericVector a, NumericVector b) {
  Rcpp::NumericVector c(a.size() + b.size());
  int j=0;
  for (int i=0; i<a.size(); i++)
    c[j++] += a[i];
  for (int i=0; i<b.size(); i++)
    c[j++] += b[i];
  return c;
}')

4.0.1. Function returning a number

Rcpp::cppFunction('double cppIntSum(NumericVector x) {
  double total = 0;
  for(int i=0; i<x.size(); ++i) {
    total += x[i];
  }
  return total;
}')

5. Calling our functions

Here we call our concatination functions (the internal one and external one):

cppIntCat(1:10, 2:11)
[1]  1  2  3  4  5  6  7  8  9 10  2  3  4  5  6  7  8  9 10 11
cppExtCat(1:10, 2:11)
[1]  1  2  3  4  5  6  7  8  9 10  2  3  4  5  6  7  8  9 10 11

Here we call our sum functions (the internal one and external one):

cppIntSum(1:10)
[1] 55
cppExtSum(1:10)
[1] 55