[Rcpp-devel] Dynamic Wrapping and Recursion with Rcpp - using an R function?

Tal Galili tal.galili at gmail.com
Fri Jul 26 17:06:47 CEST 2013


Hello all,

I would like to write an Rcpp function that will get an R object, an R
function, and will use the function on the object through Rcpp.
For example:

library(Rcpp)
cppFunction('NumericVector runRfuncSum(NumericVector x, Function fu) {
            NumericVector fu_x = fu(x);
            return fu_x;
            }')
runRfuncSum(c(1:4), sum)

However, following the example given here:
http://gallery.rcpp.org/articles/rcpp-wrap-and-recurse/
I would like this function to get an R List (specifically a dendrogram),
and will recursively go through all of the dendrogram nodes and apply the
function.
I can't seem to get it to work.

For example:



cppFunction('
            List dendrapply_Cpp_fun(List x, Function FUN) {

            // note: The FUN will change the object x itself
            // hence - it must be clear to use an object we wish to change
(it will NOT be copied!)

            for( List::iterator it = x.begin(); it != x.end(); ++it ) {
            *it = FUN(*it);
            *it = dendrapply_Cpp_fun(*it, FUN);
            }

            return x;
            }')
fu <- function(node) {
   attr(node, "height") = attr(node, "height")+1
}
dend <- as.dendrogram(hclust(dist(USArrests[1:3,]), "ave"))
dendrapply_Cpp_fun(dend, fu)


Any suggestion as to why this is not working?

Thanks!






----------------Contact
Details:-------------------------------------------------------
Contact me: Tal.Galili at gmail.com |
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
----------------------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130726/076475c1/attachment.html>


More information about the Rcpp-devel mailing list