File: cxxfunction.Rd

package info (click to toggle)
r-cran-inline 0.3.21-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 188 kB
  • sloc: makefile: 2
file content (63 lines) | stat: -rw-r--r-- 2,046 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
\name{cxxfunction}
\alias{cxxfunction}
\alias{rcpp}
\title{inline C++ function}
\description{
Functionality to dynamically define an R function with inlined C++ code
using the \code{\link{.Call}} calling convention.

The \code{rcpp()} wrapper sets the plugin to the \dQuote{Rcpp} value
suitable for using \pkg{Rcpp}.
}
\usage{
cxxfunction(sig = character(), body = character(), 
            plugin = "default", includes = "", 
            settings = getPlugin(plugin), ..., verbose = FALSE)
rcpp(..., plugin="Rcpp")
}
\arguments{
  \item{sig}{Signature of the function. A named character vector}
  \item{body}{A character vector with C++ code to include in the body of the compiled C++ function}
  \item{plugin}{Name of the plugin to use. See \code{\link{getPlugin}} for details about plugins. }
  \item{includes}{User includes, inserted after the includes provided by the plugin. }
  \item{settings}{Result of the call to the plugin}
  \item{\dots}{Further arguments to the plugin}
  \item{verbose}{verbose output}
}
\value{A function}
\seealso{\code{\link{cfunction}}}
\examples{
\dontrun{
# default plugin
fx <- cxxfunction(signature(x = "integer", y = "numeric"), 
	          "return Rf_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)
}
}
}
\keyword{programming}
\keyword{interface}