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:
- 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"))
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).
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 functionfind.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 theRcpp
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 theinst
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?