The reason I am interested in using call by reference is that I am accessing data frames with over a million rows and hundreds of columns. It is more efficient to operate on such a data frame directly, as opposed to copying it into and out of a function. In other words, I want to be *not* like R, which is why I am interested in utilizing C++, which supports call by reference.
On Wed, Jul 29, 2015 at 12:37 PM, Dirk Eddelbuettel <e...@debian.org> wrote: > > On 29 July 2015 at 07:29, Rguy wrote: > | Thanks for the insight. For other readers possibly struggling with call > by > | reference, the following code works as expected, i.e., x is changed to > c(1, 2). > > Nobody is struggling, but your example is still "unusual" (to avoid the > loaded term "wrong") as we generally prefer functions which compute > something > to return something. > > But if you insist on side-effects, this is simpler: > > R> cppFunction("void myabs(NumericVector & x) { x = abs(x); }") > R> x <- c(-2.0, 4.0) > R> myabs(x) > >R x > [1] 2 4 > R> > > But you should really do > > R> cppFunction("NumericVector myabs(NumericVector & x) { return abs(x); }") > R> x <- c(-2.0, 4.0) > R> myabs(x) > [1] 2 4 > R> > > which more like R. > > Dirk > > -- > http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org >
_______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel