[Rcpp-commits] r607 - pkg/inst/examples/functionCallback

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Feb 6 18:54:15 CET 2010


Author: edd
Date: 2010-02-06 18:54:14 +0100 (Sat, 06 Feb 2010)
New Revision: 607

Added:
   pkg/inst/examples/functionCallback/newApiExample.r
Log:
new and _much simpler_ example


Added: pkg/inst/examples/functionCallback/newApiExample.r
===================================================================
--- pkg/inst/examples/functionCallback/newApiExample.r	                        (rev 0)
+++ pkg/inst/examples/functionCallback/newApiExample.r	2010-02-06 17:54:14 UTC (rev 607)
@@ -0,0 +1,39 @@
+#!/usr/bin/r -ti
+
+suppressMessages(library(Rcpp))
+suppressMessages(library(inline))
+
+# R function that will be called from C++
+vecfunc <- function(x) {
+    y <- x^1.05                         # do a transformation
+    print(y)                            # but also print
+    plot(y, ylim=c(1,8), type='b')      # and even plot
+    Sys.sleep(0.225)                    # sleep before next call
+    return(y)
+}
+
+# C++ source code to operate on function and vector
+cpp <- '
+	int n = as<int>(N);
+	NumericVector numvec(xvec) ;
+	Function f(fun) ;
+	for( int i=0; i<n; i++){
+		numvec = f( numvec ) ;
+	}
+	return numvec ;
+'
+
+# create a C++ function
+funx <- cfunction(signature(N = "integer" , xvec = "numeric", fun = "function" ),
+                  cpp, , Rcpp = TRUE, include = "using namespace Rcpp; ")
+
+# create the vector
+xvec <- sqrt(c(1:12, 11:1))
+
+# set up x11
+x11(width=3,height=3)
+par(mar=c(3,3,1,1),cex=0.8, pch=19)
+
+# run example
+funx( 10L, xvec, vecfunc )
+


Property changes on: pkg/inst/examples/functionCallback/newApiExample.r
___________________________________________________________________
Name: svn:executable
   + *



More information about the Rcpp-commits mailing list