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

Reply via email to