Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

cxxfunction

inline C++ function


Description

Functionality to dynamically define an R function with inlined C++ code using the .Call calling convention.

The rcpp() wrapper sets the plugin to the “Rcpp” value suitable for using Rcpp.

Usage

cxxfunction(sig = character(), body = character(), 
            plugin = "default", includes = "", 
            settings = getPlugin(plugin), ..., verbose = FALSE)
rcpp(..., plugin="Rcpp")

Arguments

sig

Signature of the function. A named character vector

body

A character vector with C++ code to include in the body of the compiled C++ function

plugin

Name of the plugin to use. See getPlugin for details about plugins.

includes

User includes, inserted after the includes provided by the plugin.

settings

Result of the call to the plugin

...

Further arguments to the plugin

verbose

verbose output

Value

A function

See Also

Examples

## Not run: 
# default plugin
fx <- cxxfunction(signature(x = "integer", y = "numeric"), 
	          "return ScalarReal(INTEGER(x)[0] * REAL(y)[0]);")
fx(2L, 5)

# Rcpp plugin
if (requireNamespace("Rcpp", quietly=TRUE)) {

    fx <- cxxfunction(signature(x = "integer", y = "numeric"), 
                      "return wrap( as<int>(x) * as<double>(y));",
                      plugin = "Rcpp" )
    fx(2L, 5)

    ## equivalent shorter form using rcpp()
    fx <- rcpp(signature(x = "integer", y = "numeric"),
               "return wrap(as<int>(x) * as<double>(y));")
}

# RcppArmadillo plugin
if (requireNamespace(RcppArmadillo)) {
	
    fx <- cxxfunction(signature(x = "integer", y = "numeric"),
                      "int dim = as<int>(x);
		       arma::mat z = as<double>(y) * arma::eye<arma::mat>(dim, dim);
		       return wrap(arma::accu(z));",
                      plugin = "RcppArmadillo")
    fx(2L, 5)
}

## End(Not run)

inline

Functions to Inline C, C++, Fortran Function Calls from R

v0.3.17
LGPL
Authors
Oleg Sklyar, Duncan Murdoch, Mike Smith, Dirk Eddelbuettel, Romain Francois, Karline Soetaert, Johannes Ranke
Initial release
2020-11-30

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.