Rcpp R / C++ interface example – deprecated API

Description

RcppExample illustrates how the older Rcpp R/C++ interface class library is used. It provides fairly complete coverage for the older ‘classic’ API.

Note that the RcppClassic package has been deprecated since 2010, all new development should use the Rcpp package instead.

Usage

1
2
3
RcppExample(params, nlist, numvec, nummat, df, datevec, stringvec, fnvec, fnlist)
## S3 method for class 'RcppExample'
print(x,...)

Arguments

params

A heterogeneous list specifying method (string), tolerance (double), maxIter (int).

nlist

a list of named numeric values (double or int).

numvec

a numeric 1D vector (double or int).

nummat

a numeric 2D matrix (double or int).

df

a data frame.

datevec

a vector of Date's.

stringvec

a vector of strings.

fnvec

an R function with numeric vector argument.

fnlist

an R function with list argument.

x

Object of type RcppExample.

...

Extra named parameters.

Details

The C++ represention of data frames are not passed back to R in a form that R recognizes as a data frame, but it is a simple matter to do the conversion. For example, the return value named PreDF (see return values below) is not seen as a data frame on the R side (thus the name "pre-data frame"), but it can be converted to a data frame using df <- data.frame(result$PreDF).

The print.RcppExample() function is defined so that we can control what gets printed when a variable assigned the return value is entered on a line by itself. It is defined to simply list the names of the fields returned (see RcppExample.R).

Value

RcppExample returns a list containing:

method

string input paramter

tolerance

double input paramter

maxIter

int input parameter

nlFirstName

first name in nlist

nlFirstValue

first value in nlist

matD

R matrix from an RcppMatrix<double> object

stlvec

R vector from a vector<double> object

stlmat

R matrix from a vector<vector<double> > object

a

R matrix from C/C++ matrix

v

R vector from C/C++ vector

strings

R vector of strings from vector<string> object

InputDF

a data frame passed in from R

PreDF

a data frame created on C++ side to be passed back to R

params

input parameter list (this is redundant because we returned the input parameters above)

Author(s)

Dominick Samperi wrote the initial versions of Rcpp (and RcppTemplate) during 2005 and 2006. Dirk Eddelbuettel made some additions, and became maintainer in 2008. Dirk Eddelbuettel and Romain Francois have been extending Rcpp since 2009.

References

Writing R Extensions, available at http:www.r-project.org.

See Also

See the RcppExamples-package for examples of the recommended Rcpp API and Rcpp-package for documentation on the recommended API to extend R with C++ code, while the deprecated RcppClassic-package documents the older, deprecated API.

Examples

 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
params <- list(method='BFGS',
               tolerance=1.0e-8,
               maxIter=1000,
               startDate=as.Date('2006-7-15'))

nlist <- list(ibm = 80.50, hp = 53.64, c = 45.41)

numvec <- seq(1,5) # numerical vector

nummat <- matrix(seq(1,20),4,5) # numerical matrix

stringvec <- c("hello", "world", "fractal") # string vector

datestr <- c('2006-6-10', '2006-7-12', '2006-8-10')
datevec <- as.Date(datestr, "%Y-%m-%d") # date vector

df <- data.frame(a=c(TRUE, TRUE, FALSE), b=I(c('a','b','c')),
c=c('beta', 'beta', 'gamma'), dates=datevec)

fnvec <- function(x) { sum(x) } # Add up components of vector

fnlist <- function(l) { # Return vector with 1 added to each component
  vec <- c(l$alpha + 1, l$beta + 1, l$gamma + 1)
  vec
}

result <- RcppExample(params, nlist, numvec, nummat, df, datevec,
                      stringvec, fnvec, fnlist)

result