[Rcpp-devel] passing function

Dirk Eddelbuettel edd at debian.org
Wed Jan 26 16:38:34 CET 2011


Dear Sid,

On 26 January 2011 at 09:23, Siddhartha Chib wrote:
| Thanks.
| 
|  I did not understand this code
| 
|  # 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 ;
| 
|  I see that f is a Function object but what is fun? And why the looping over numvec rather than elements of numvec? Finally, how to use RcppArmadillo to do the C++ computations on the R function?

You are quoting code out of context which makes it hard to answer it. So
allow me to be verbose and to include the full example file
inst/examples/functionCallback/newApiExample.r (which gets installed in 
Rcpp/examples/functionCallback/newApiExample.r along with Rcpp):

-----------------------------------------------------------------------------
#!/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 )


-----------------------------------------------------------------------------

Now for your questions:

    I see that f is a Function object but what is fun? 

There is no 'fun' but 'funx' is an R function created by cfunction() [ of the
inline package ] containing executable code created by cfunction() from the
source code submitted via variable 'cpp'.

    And why the looping over numvec rather than elements of numvec? 

Try to run the example (eg by executing 'Rscript newApiExample.r' or if you
have littler (eg on Linux) via 'r newApiExample.r' or simply by making the
script executable.  

What it does is to repeatedly plot transformations of a vector (see vecfun()
which is an R function you could run standalone).  

If you wanted to, you could work element by element with another loop.  Here
we show how to pass a whole vector.

    Finally, how to use RcppArmadillo to do the C++ computations on the R function?

Sure -- just instantiate RcppArmadillo objects rather than Rcpp objects.
When this example was written, RcppArmadillo did not yet exist -- but there
is no fundamental difference.  And there are plenty of RcppArmadillo example
to go around.

I hope this helps.  Thanks for your continued interest in Rcpp.

Best regards, Dirk


|  Best.
| 
| 
| 
| On 1/25/2011 12:17 PM, Dirk Eddelbuettel wrote:
| 
|     On 25 January 2011 at 12:01, Siddhartha Chib wrote:
|     | Is it possible to pass a user defined function
|     | written in R for use in rccparma.  For example, is it possible to have a
| 
|     (What is rccparma?)
| 
|     | function
|     | SEXP myfunction(SEXP g, SEXP a)
|     | where g is a function object and a is a
|     | matrix, the argument of g?
| 
|     Have you looked at
| 
|          examples/functionCallback/newApiExample.r
| 
|     which installed with your copy of Rcpp -- it uses Rcpp to sweep a
|     user-supplied R function over a vector.  Doing this with a matrix is close.
| 
|     And just for completeness, there is more in other place; my RcppDE riff on
|     the the DEoptim software allows you to pass objective functions as either R
|     or C++ functions; the C++ side then needs external pointers.
| 
|     Hope this helps,  Dirk
| 
| 
| 
| 
| --
| Siddhartha Chib
| Harry C. Hartkopf Professor of Econometrics and Statistics
| Olin Business School, Campus Box 1133
| Washington University in St. Louis
| 1 Brookings Dr
| St. Louis, MO 63130
| (314) 935-4657;    (314) 935-6359 [Fax]
| chib at wustl.edu
| http://www.olin.wustl.edu/faculty/chib/
| 

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list