Translate R functions to C++