[Rcpp-devel] Call by reference

Rguy rguy at 123mail.org
Wed Jul 29 18:28:17 CEST 2015


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 <edd at 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 | edd at debian.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150729/6f3b2544/attachment.html>


More information about the Rcpp-devel mailing list