EVOLUTION-MANAGER
Edit File: cppFunction.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Define an R Function with a C++ Implementation</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for cppFunction {Rcpp}"><tr><td>cppFunction {Rcpp}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2> Define an R Function with a C++ Implementation </h2> <h3>Description</h3> <p>Dynamically define an R function with C++ source code. Compiles and links a shared library with bindings to the C++ function then defines an R function that uses <code>.Call</code> to invoke the library. </p> <h3>Usage</h3> <pre> cppFunction(code, depends = character(), plugins = character(), includes = character(), env = parent.frame(), rebuild = FALSE, cacheDir = getOption("rcpp.cache.dir", tempdir()), showOutput = verbose, verbose = getOption("verbose")) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>code</code></td> <td> <p>Source code for the function definition. </p> </td></tr> <tr valign="top"><td><code>depends</code></td> <td> <p>Character vector of packages that the compilation depends on. Each package listed will first be queried for an <a href="../../inline/html/plugins.html">inline plugin</a> to determine header files to include. If no plugin is defined for the package then a header file based the package's name (e.g. <code>PkgName.h</code>) will be included. </p> </td></tr> <tr valign="top"><td><code>plugins</code></td> <td> <p>Character vector of <a href="../../inline/html/plugins.html">inline plugins</a> to use for the compilation. </p> </td></tr> <tr valign="top"><td><code>includes</code></td> <td> <p>Character vector of user includes (inserted after the includes provided by <code>depends</code>). </p> </td></tr> <tr valign="top"><td><code>env</code></td> <td> <p>The <a href="../../base/html/environment.html">environment</a> in which to define the R function. May be <code>NULL</code> in which case the defined function can be obtained from the return value of <code>cppFunction</code>. </p> </td></tr> <tr valign="top"><td><code>rebuild</code></td> <td> <p>Force a rebuild of the shared library. </p> </td></tr> <tr valign="top"><td><code>cacheDir</code></td> <td> <p>Directory to use for caching shared libraries. If the underlying code passed to <code>sourceCpp</code> has not changed since the last invocation then a cached version of the shared library is used. The default value of <code>tempdir()</code> results in the cache being valid only for the current R session. Pass an alternate directory to preserve the cache across R sessions. </p> </td></tr> <tr valign="top"><td><code>showOutput</code></td> <td> <p><code>TRUE</code> to print <code>R CMD SHLIB</code> output to the console. </p> </td></tr> <tr valign="top"><td><code>verbose</code></td> <td> <p><code>TRUE</code> to print detailed information about generated code to the console. </p> </td></tr> </table> <h3>Details</h3> <p>Functions defined using <code>cppFunction</code> must have return types that are compatible with <code>Rcpp::wrap</code> and parameter types that are compatible with <code>Rcpp::as</code>. </p> <p>The shared library will not be rebuilt if the underlying code has not changed since the last compilation. </p> <h3>Value</h3> <p>An R function that uses <code>.Call</code> to invoke the underlying C++ function. </p> <h3>Note</h3> <p>You can also define R functions with C++ implementations using the <code><a href="sourceCpp.html">sourceCpp</a></code> function, which allows you to separate the C++ code into it's own source file. For many use cases this is an easier and more maintainable approach. </p> <h3>See Also</h3> <p><code><a href="sourceCpp.html">sourceCpp</a></code>, <code><a href="evalCpp.html">evalCpp</a></code> </p> <h3>Examples</h3> <pre> ## Not run: cppFunction( 'int fibonacci(const int x) { if (x == 0) return(0); if (x == 1) return(1); return (fibonacci(x - 1)) + fibonacci(x - 2); }') cppFunction(depends = "RcppArmadillo", 'List fastLm(NumericVector yr, NumericMatrix Xr) { int n = Xr.nrow(), k = Xr.ncol(); arma::mat X(Xr.begin(), n, k, false); arma::colvec y(yr.begin(), yr.size(), false); arma::colvec coef = arma::solve(X, y); arma::colvec resid = y - X*coef; double sig2 = arma::as_scalar(arma::trans(resid)*resid/(n-k) ); arma::colvec stderrest = arma::sqrt( sig2 * arma::diagvec(arma::inv(arma::trans(X)*X))); return List::create(Named("coefficients") = coef, Named("stderr") = stderrest ); }') cppFunction(plugins=c("cpp11"), ' int useCpp11() { auto x = 10; return x; }') ## End(Not run) </pre> <hr /><div style="text-align: center;">[Package <em>Rcpp</em> version 1.0.5 <a href="00Index.html">Index</a>]</div> </body></html>