<div dir="ltr"><div>Hello all,</div><div><br></div><div>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.</div><div>For example:</div><div>
<br></div><div><div>library(Rcpp)</div><div>cppFunction('NumericVector runRfuncSum(NumericVector x, Function fu) {</div><div> NumericVector fu_x = fu(x); </div><div> return fu_x; </div><div>
}')</div>
<div>runRfuncSum(c(1:4), sum)</div></div><div><br></div><div>However, following the example given here:</div><div><a href="http://gallery.rcpp.org/articles/rcpp-wrap-and-recurse/">http://gallery.rcpp.org/articles/rcpp-wrap-and-recurse/</a><br>
</div><div>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.</div><div>I can't seem to get it to work.</div><div>
<br></div><div>For example:</div><div><br></div><div><div><br></div><div><br></div><div>cppFunction('</div><div> List dendrapply_Cpp_fun(List x, Function FUN) {</div><div> </div><div> // note: The FUN will change the object x itself</div>
<div> // hence - it must be clear to use an object we wish to change (it will NOT be copied!)</div><div> </div><div> for( List::iterator it = x.begin(); it != x.end(); ++it ) {</div><div>
*it = FUN(*it);</div><div> *it = dendrapply_Cpp_fun(*it, FUN);</div><div> }</div><div> </div><div> return x; </div><div> }')</div><div>fu <- function(node) { </div>
<div> attr(node, "height") = attr(node, "height")+1</div><div>}</div><div>dend <- as.dendrogram(hclust(dist(USArrests[1:3,]), "ave"))</div><div>dendrapply_Cpp_fun(dend, fu)</div></div><div>
<br></div><div><br></div><div>Any suggestion as to why this is not working?</div><div><br></div><div>Thanks!</div><div><br></div><div><br></div><div><br></div><div><br></div><br clear="all"><div><div dir="ltr"><br>----------------Contact Details:-------------------------------------------------------<br>
Contact me: <a href="mailto:Tal.Galili@gmail.com" target="_blank">Tal.Galili@gmail.com</a> | <br>Read me: <a href="http://www.talgalili.com" target="_blank">www.talgalili.com</a> (Hebrew) | <a href="http://www.biostatistics.co.il" target="_blank">www.biostatistics.co.il</a> (Hebrew) | <a href="http://www.r-statistics.com" target="_blank">www.r-statistics.com</a> (English)<br>
----------------------------------------------------------------------------------------------<br><br></div></div>
</div>