<div dir="ltr">Hi Tal,<div><br></div><div>A couple points:</div><div><br></div><div>1) Your R function `fu` doesn't specify a return. In addition, calling an R function in C++ code is still going to fall into the same pitfalls of calling it directly in R -- you're going to make a copy, and going to have to reassign the copy. You can cheat by using `<<-` but this is not recommended.</div>



<div><br></div><div>Case-in-point:</div><div><br></div><div>cppFunction('void test(NumericVector x, Function FUN) { FUN(x); }')</div><div>x <- c(1, 2, 3)</div><div>f <- function(x) { x <- x + 100 }</div>


<div>g <- function(x) { return(x + 100) }</div><div>h <- function(x) { x <<- x + 100 }</div><div>test(x, f)</div><div>x ## no change</div><div>test(x, g)</div><div>x ## no change</div><div>test(x, h)</div><div>

x ## changed</div><div><br></div><div>If you care about speed, passing R functions to your Rcpp code is not recommended.</div><div><br></div><div>2) You don't switch over the RTYPE in your for loop. The elements in your list are either going to be lists (VECSXPs) if they're another branch of the tree, or non-lists otherwise (probably numeric / REALSXP?). I imagine the behavior you want depends on the type of object you encounter while recursing.</div>



<div><br></div><div>If you really want to do things in place and do it quickly, do it entirely in C++. Calling R functions from C++ isn't going to help. </div><div><br></div><div>Finally, if you want to set attributes explicitly, check out the `.attr` function.</div>



<div><br></div><div>-Kevin</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jul 26, 2013 at 2:06 PM, Krzysztof Sakrejda <span dir="ltr"><<a href="mailto:krzysztof.sakrejda@gmail.com" target="_blank">krzysztof.sakrejda@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Make the wrapper a reference class which has a field for the data.<br>
When the wrapper is initialized, pass the data as an argument and it<br>
will be copied when the initialize method runs. Works in my project...<br>
or am I missing something here?<br>
<br>
Krzysztof<br>
<div><div class="h5"><br>
On Fri, Jul 26, 2013 at 4:58 PM, Tal Galili <<a href="mailto:tal.galili@gmail.com">tal.galili@gmail.com</a>> wrote:<br>
> Hi Hadley,<br>
><br>
> Regarding the cloning of the object in C++: since I will want to use the<br>
> function recursively, I will either have the cloning in the wrapper R<br>
> function. And if I discover that is not possible (I haven't tried it yet),<br>
> my next thought was to split the work into two functions, in one there will<br>
> be the cloning, and the other will do the recursion.<br>
> Since the example I gave in the first e-mail doesn't work (help is<br>
> welcomed), I am not there yet :)<br>
><br>
> As for as.dendrogram, that is the case with hclust object. However, these<br>
> objects has limitations when it comes to trees which are NOT binary trees.<br>
> So one of the things I started doing with dendextend is to create various<br>
> functions that will work for non-binary trees (which I previously had to<br>
> work with).<br>
><br>
> Tal<br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
> ----------------Contact<br>
> Details:-------------------------------------------------------<br>
> Contact me: <a href="mailto:Tal.Galili@gmail.com">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) |<br>
> <a href="http://www.r-statistics.com" target="_blank">www.r-statistics.com</a> (English)<br>
> ----------------------------------------------------------------------------------------------<br>
><br>
><br>
><br>
> On Fri, Jul 26, 2013 at 7:07 PM, Hadley Wickham <<a href="mailto:h.wickham@gmail.com">h.wickham@gmail.com</a>> wrote:<br>
>><br>
>> > I understand your point, and am not sure how to proceed without it.<br>
>><br>
>> Just make sure you clone the initial list once, or use another C++<br>
>> data structure (e.g. std::vector) that can grow efficiently.<br>
>><br>
>> Alternatively, you could create your own alternative to as.dendrogram<br>
>> that doesn't create a hierarchical structure.  (That's what I'd try<br>
>> first)<br>
>><br>
>> Hadley<br>
>><br>
>> --<br>
>> Chief Scientist, RStudio<br>
>> <a href="http://had.co.nz/" target="_blank">http://had.co.nz/</a><br>
><br>
><br>
><br>
</div></div><div class="im">> _______________________________________________<br>
> Rcpp-devel mailing list<br>
> <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
> <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
<br>
<br>
<br>
</div>--<br>
<br>
Krzysztof Sakrejda<br>
<br>
Organismic and Evolutionary Biology<br>
University of Massachusetts, Amherst<br>
319 Morrill Science Center South<br>
611 N. Pleasant Street<br>
Amherst, MA 01003<br>
<br>
work #: <a href="tel:413-325-6555" value="+14133256555">413-325-6555</a><br>
email: <a href="mailto:sakrejda@cns.umass.edu">sakrejda@cns.umass.edu</a><br>
-----------------------------------------------<br>
<div class="HOEnZb"><div class="h5">_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</div></div></blockquote></div><br></div>