[Rcpp-devel] Dynamic Wrapping and Recursion with Rcpp - using an R function?
Romain Francois
romain at r-enthusiasts.com
Sat Jul 27 12:52:30 CEST 2013
A bit of warning from a different angle.
Do you really want to be able to pass any arbitrary R function to your
dendrapply ? You just can't expect R functions to work "in place".
e.g. your function fu:
fu <- function(node) {
attr(node, "height") = attr(node, "height")+1
}
Does not modify its argument. It creates a copy, modifies the copy, and
then returns (invisibly) the result of the expression, and then the copy
is forgotten about, it just becomes a candidate for the next garbage
collection.
As for the design of what you are doing, I would suggest:
- having one C++ function that would start by creating a clone of your
list, using clone<List>.
- from this call a C++ function to recurse.
- for the actual function that does the modifying in place, I would
suggest this is a C++ function. But this enters into the world of
passing around C++ function pointers. Not sure you want to travel there
yet.
Another thing could be not passing a function to execute, but some
string identifying what it does, and then internally you can dispatch to
the function.
Not sure this makes a lot of sense. Anyway, I think my question is:
do you really want the flexibility of being able to call any R function,
or can you come up with a list of operations you'd do on terminal nodes
of your tree ?
what do you want to do with your dendrograms ?
Romain
Le 26/07/13 17:06, Tal Galili a écrit :
> 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!
--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
R Graph Gallery: http://gallery.r-enthusiasts.com
blog: http://blog.r-enthusiasts.com
|- http://bit.ly/13SrjxO : highlight 0.4.2
`- http://bit.ly/10X94UM : Mobile version of the graph gallery
More information about the Rcpp-devel
mailing list