From edd at debian.org  Tue Jul  8 17:18:54 2014
From: edd at debian.org (Dirk Eddelbuettel)
Date: Tue, 8 Jul 2014 10:18:54 -0500
Subject: [Rd] arguments to .Call(), .External should be read-only?
In-Reply-To: <53BBFE66.8030308@gmail.com>
References: <82683.1404828303@greg-minshalls-mbp.local>
	<53BBFE66.8030308@gmail.com>
Message-ID: <21436.3038.755092.674110@max.nulle.part>


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 | edd at debian.org



[Rd] arguments to .Call(), .External should be read-only?
Dirk Eddelbuettel