From tlumley@u.washington.edu  Mon Mar 26 22:05:16 2001
From: tlumley@u.washington.edu (Thomas Lumley)
Date: Mon, 26 Mar 2001 13:05:16 -0800 (PST)
Subject: [Rd] DUP=FALSE
Message-ID: <Pine.A41.4.33.0103261251200.14166-100000@homer16.u.washington.edu>


Last week Paul Gilbert asked about DUP=FALSE and whether parent
environments can be affected.  I have experimented and they can.
Consider the R functions
> f
function(x){ .C("badidea",x,DUP=FALSE)}
> g
function(y) f(y)
> h
function(z){
        z<-as.integer(z)
        g(z)
        z
}

and the C function
	void badidea(int *x){
	    *x=7;
	}

This gives
> h(4)
[1] 7

so the modification of x in f() reaches up past g() to modify z in h().
The conclusion is that one should not pass a formal parameter of a
function using DUP=FALSE if it might be modified.  Assigning the parameter
seems to be sufficient to prevent the problem: replacing g by

 g<-function(y) {
	w<-y
	f(w)
 }
gives
> h(4)
[1] 4
as expected.


	-thomas

Thomas Lumley			Asst. Professor, Biostatistics
tlumley@u.washington.edu	University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] DUP=FALSE
Thomas Lumley