3suppressMessages(library(Rcpp))
5# R function that will be called from C++
6vecfunc <- function(x) {
7 y <- x^1.05 # do a transformation
8 print(y) # but also print
9 plot(y, ylim=c(1,8), type='b') # and even plot
10 Sys.sleep(0.225) # sleep before next call
14## NOTE: This is the old way to compile Rcpp code inline.
15## The code here has left as a historical artifact and tribute to the old way.
16## Please use the code under the "new" inline compilation section.
18suppressMessages(library(inline))
20# C++ source code to operate on function and vector
23 NumericVector numvec(xvec) ;
25 for( int i=0; i<n; i++){
26 numvec = f( numvec ) ;
31# create a C++ function
32funx <- cxxfunction(signature(N = "integer" , xvec = "numeric", fun = "function" ),
33 body=cpp, include = "using namespace Rcpp; ", plugin = "Rcpp")
36## NOTE: Within this section, the new way to compile Rcpp code inline has been
37## written. Please use the code next as a template for your own project.
39# C++ source code to operate on function and vector
41NumericVector funx(int n, NumericVector numvec, Function f){
42 for( int i = 0; i < n; i++ ){
43 numvec = f( numvec ) ;
50xvec <- sqrt(c(1:12, 11:1))
54par(mar=c(3,3,1,1),cex=0.8, pch=19)
57funx( 10L, xvec, vecfunc )