translate {ast2ast}R Documentation

Translates a R function into a C++ function and returns an external pointer (XPtr) to this function. Further information can be found in the vignette: 'Detailed Documentation'.

Description

Translates a R function into a C++ function and returns an external pointer (XPtr) to this function. Further information can be found in the vignette: 'Detailed Documentation'.

Usage

translate(f, verbose = FALSE, reference = FALSE)

Arguments

f

The function which should be translated from R to C++.

verbose

If set to TRUE the output of RcppXPtrUtils::cppXPtr is printed.

reference

If set to true the arguments are passed by reference.

Details

The following types are supported:

  1. numeric vectors

  2. numeric matrices

Variables can be either numeric vectors or matrices. Notably, it is possible that the variables change the type within the function. It is possible to declare a variable of a scalar numeric data type. This is done by adding '_db' to the end of the variable. Each time '_db' is found the variable is declared as a scalar numeric data type. In this case the object cannot change its type!

The following functions are supported:

  1. assignment: = and <-

  2. allocation: vector and matrix

  3. information about objects: length and dim

  4. Basic operations: +, -, *, /

  5. Indices: [] and at

  6. mathematical functions: sin, asin, sinh, cos, acos, cosh, tan, atan, tanh, log, ^ and exp

  7. concatenate objects: c

  8. comparison: ==, !=, >, <, >= and <=

  9. printing: print

  10. returning objects: return

  11. catmull-rome spline: cmr

  12. to get a range of numbers the ':' function can be used

Some details about the implemented functions

Be aware that the R code is translated to ETR an expression template library which tries to mimic R. However, it does not behave exactly like R! Please check your compiled function before using it in a serious project. If you want to see how ast2ast differs from R in detail check the vignette: 'Detailed Documentation'.

Value

The external pointer of the generated C++ function

Examples

#Further examples can be found in the vignette: 'Detailed Documentation'.
#Hello World
## Not run: 
f <- function() { print("Hello World!")}
pointer_to_f_cpp <- ast2ast::translate(f)
Rcpp::sourceCpp(code = "
#include <Rcpp.h>
typedef void (*fp)();

// [[Rcpp::export]]
void call_fct(Rcpp::XPtr<fp> inp) {
  fp f = *inp;
  f();
}
")
call_fct(pointer_to_f_cpp)

## End(Not run)

[Package ast2ast version 0.1 Index]