|
Rcpp Version 0.9.10
|
00001 #!/usr/bin/r -ti 00002 00003 suppressMessages(library(Rcpp)) 00004 suppressMessages(library(inline)) 00005 00006 # R function that will be called from C++ 00007 vecfunc <- function(x) { 00008 y <- x^1.05 # do a transformation 00009 print(y) # but also print 00010 plot(y, ylim=c(1,8), type='b') # and even plot 00011 Sys.sleep(0.225) # sleep before next call 00012 return(y) 00013 } 00014 00015 # C++ source code to operate on function and vector 00016 cpp <- ' 00017 int n = as<int>(N); 00018 NumericVector numvec(xvec) ; 00019 Function f(fun) ; 00020 for( int i=0; i<n; i++){ 00021 numvec = f( numvec ) ; 00022 } 00023 return numvec ; 00024 ' 00025 00026 # create a C++ function 00027 funx <- cfunction(signature(N = "integer" , xvec = "numeric", fun = "function" ), 00028 cpp, , Rcpp = TRUE, include = "using namespace Rcpp; ") 00029 00030 # create the vector 00031 xvec <- sqrt(c(1:12, 11:1)) 00032 00033 # set up x11 00034 x11(width=3,height=3) 00035 par(mar=c(3,3,1,1),cex=0.8, pch=19) 00036 00037 # run example 00038 funx( 10L, xvec, vecfunc ) 00039