sourceCpp is the important function in Rcpp. The Rcpp code is written into file with'.cpp' extension. The cppFunction can be useful but we can not easily combine many functions in the same time. This is an example to use the two possibility:
- ccpFunction
R code:
Rcpp::cppFunction(
'int fibonacci(const int x) {
if (x == 0) return(0);
if (x == 1) return(1);
return (fibonacci(x - 1)) + fibonacci(x - 2);
}')
fibonacci(20)
- sourceCpp
I used the example used in the Rstudio C++ source file.
Get the source file: firstrun.cpp and save it in the same R directory.
R code:
Rcpp::sourceCpp('firstrun.cpp')
No comments:
Post a Comment