It's one of my favourite Rcpp puzzles:

R> cppFunction('bool foo(NumericVector x) { x = 2 * x; return(true); }')
R> x <- 1:4                     # note that x is now an int vector
R> foo(x)                       # so this casts to numeric --> copy 
[1] TRUE
R> x
[1] 1 2 3 4                     # still the same
R> x <- 1:4 * 1.0               # BUT now we create a numeric vector
R> foo(x)
[1] TRUE
R> x                            # and that one DOES get modified
[1] 2 4 6 8
R> 

Morale: The P in SEXP means that you get to change the external object.  So
as Duncan and the WRE manual say: proceed with some caution.

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to