[Rcpp-devel] Dynamic Wrapping and Recursion with Rcpp - using an R function?
Kevin Ushey
kevinushey at gmail.com
Fri Jul 26 23:51:19 CEST 2013
Hi Tal,
A couple points:
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.
Case-in-point:
cppFunction('void test(NumericVector x, Function FUN) { FUN(x); }')
x <- c(1, 2, 3)
f <- function(x) { x <- x + 100 }
g <- function(x) { return(x + 100) }
h <- function(x) { x <<- x + 100 }
test(x, f)
x ## no change
test(x, g)
x ## no change
test(x, h)
x ## changed
If you care about speed, passing R functions to your Rcpp code is not
recommended.
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.
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.
Finally, if you want to set attributes explicitly, check out the `.attr`
function.
-Kevin
On Fri, Jul 26, 2013 at 2:06 PM, Krzysztof Sakrejda <
krzysztof.sakrejda at gmail.com> wrote:
> Make the wrapper a reference class which has a field for the data.
> When the wrapper is initialized, pass the data as an argument and it
> will be copied when the initialize method runs. Works in my project...
> or am I missing something here?
>
> Krzysztof
>
> On Fri, Jul 26, 2013 at 4:58 PM, Tal Galili <tal.galili at gmail.com> wrote:
> > Hi Hadley,
> >
> > Regarding the cloning of the object in C++: since I will want to use the
> > function recursively, I will either have the cloning in the wrapper R
> > function. And if I discover that is not possible (I haven't tried it
> yet),
> > my next thought was to split the work into two functions, in one there
> will
> > be the cloning, and the other will do the recursion.
> > Since the example I gave in the first e-mail doesn't work (help is
> > welcomed), I am not there yet :)
> >
> > As for as.dendrogram, that is the case with hclust object. However, these
> > objects has limitations when it comes to trees which are NOT binary
> trees.
> > So one of the things I started doing with dendextend is to create various
> > functions that will work for non-binary trees (which I previously had to
> > work with).
> >
> > Tal
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > ----------------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)
> >
> ----------------------------------------------------------------------------------------------
> >
> >
> >
> > On Fri, Jul 26, 2013 at 7:07 PM, Hadley Wickham <h.wickham at gmail.com>
> wrote:
> >>
> >> > I understand your point, and am not sure how to proceed without it.
> >>
> >> Just make sure you clone the initial list once, or use another C++
> >> data structure (e.g. std::vector) that can grow efficiently.
> >>
> >> Alternatively, you could create your own alternative to as.dendrogram
> >> that doesn't create a hierarchical structure. (That's what I'd try
> >> first)
> >>
> >> Hadley
> >>
> >> --
> >> Chief Scientist, RStudio
> >> http://had.co.nz/
> >
> >
> >
> > _______________________________________________
> > Rcpp-devel mailing list
> > Rcpp-devel at lists.r-forge.r-project.org
> > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
>
>
> --
>
> Krzysztof Sakrejda
>
> Organismic and Evolutionary Biology
> University of Massachusetts, Amherst
> 319 Morrill Science Center South
> 611 N. Pleasant Street
> Amherst, MA 01003
>
> work #: 413-325-6555
> email: sakrejda at cns.umass.edu
> -----------------------------------------------
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130726/f0bbe3c8/attachment.html>
More information about the Rcpp-devel
mailing list