[Rcpp-devel] Function changing the value of objects in global environment

Nick Menzies nick.menzies at gmail.com
Sun Feb 2 02:14:03 CET 2014


Hi all,

I am relatively new to Rcpp and a bit confused about the behavior I am
seeing from an Rcpp function. It seems that a function created with Rcpp
can modify the value of objects in the global environment, yet this
behavior depended on the nature of the object. I realize I could code
things differently so that the object in the global environment is not
affected, but I wanted to make sure there is not something more general I
am doing wrong (and which will continue to be wrong without me knowing it!)

Below is a small example where the global value gets altered, followed by
an example where it does not. The only difference between them is that in
the first (cTest1) the input is NumericVector, while in the second (cTest2)
the input is double.

### Rcpp function alters values in global environment:
cppFunction('
  double cTest1( NumericVector zzz) {
    zzz = zzz/10;
    return 1;
   }')

  zzz=rep(10,3)
  zzz
  cTest1(zzz)
  zzz

### Rcpp function does not alter values in global environment:
cppFunction('
  double cTest2( double zzz) {
     zzz = zzz/10;
     return 1;
   }')

zzz=10
cTest2(zzz)
zzz
####################


Thanks for your help, and sorry for the naive question.

Nick

PS - I am running Rcpp 0.10.6, R version 3.0.1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140201/13c3287a/attachment.html>


More information about the Rcpp-devel mailing list