1

I am writing an R package that should be able to compile C++ code on the fly. In practice, users can define, at run-time, operators based on C++ code that is compiled and then used in computation (for efficiency purpose, like PyTorch or TensorFlow models in Python). Ideally, the code compiled at run-time should use Rcpp features to be exported to R.

Example:

  1. In my R package, I have a function def_operator that can parse some mathematical formula defining an operator.
my_custom_op <- def_operator("x+y", args = c("x", "y"))
  1. My Cpp API knows how to generate the Cpp code associated to this formula. This code should be compiled on the fly (just once, not at each call).

  2. The user can use this new function to do some computations.

res <- my_custom_op(1, 3) # should give 4

Note: this is an example, the operators defined by the user aim at doing more some adding scalar numbers, and the interest is clearly to let the user defines its operators and not to pre-define some generic operators compiled at installation.

I know two things for the moment:

  • the Cpp code required to generate the operators (which is not compiled at installation) should be put in the inst package directory, it will be copied at installation and I can find where with the R function find.package.
  • I can use the function sourceCpp to compile code on the fly. Thus I can define some functions in Cpp that will be automatically exported to R and be callable there. It is even possible to keep the shared library to avoid multiple compilations (see Rcpp: how to keep files generated by sourceCpp?)

Here are my questions:

  • Do you know some alternative to sourceCpp from the Rcpp package to compile C++ code on the fly and export it to R?
  • Is there some way to manage compilation option for sourceCpp other than using the file ~/.R/Makevars (I need to link the code in the inst directory and I don't want to edit this file on the user system)?
  • Eventually, do you know some R packages implementing compilation on the fly that I could take as examples?
Odin
  • 633
  • 4
  • 11

1 Answers1

4
  • Do you know some alternative to sourceCpp from the Rcpp package to compile C++ code on the fly and export it to R?

Using sourceCpp() is the best approach. Alternatively, you can use its predecessor from the inline R package. Otherwise, you will need to build your own file via R CMD SHLIB, load the library, and create a wrapper yourself. (Not fun.)

  • Is there some way to manage compilation option for sourceCpp other than using the file ~/.R/Makevars (I need to link the code in the inst directory and I don't want to edit this file on the user system)?

Yes, there are many Makevars variables that can be set per R session via Sys.setenv("PKG_LIBS" = ...).

Now, to retrieve a file location dynamically, consider RcppMLPACK1's flag function approach.

  • Eventually, do you know some R packages implementing compilation on the fly that I could take as examples?

There are a couple entrants in this market:

  • armacmp package by Dirk Schumacher that translates R code to C++ under the armadillo library.
  • nCompiler package by Perry de Valpine et al. for code-generating C++ and easily interfacing between R and C++.
coatless
  • 20,011
  • 13
  • 69
  • 84
  • 2
    Thank you very much for this complete answer. It gives me some material to continue the development of my package. – Odin Jul 31 '19 at 15:32
  • It may be a wee bit premature to recommend `armacmp`. And as it references `nCompiler` as inspiration we are really down to that one. – Dirk Eddelbuettel Jul 31 '19 at 17:54