[R] Problems with environments, I think..

Stephane DRAY dray at biomserv.univ-lyon1.fr
Thu Mar 25 15:56:56 CET 2004


Hello,
I have a general function which I am used for calculations. I call this 
function through different functions corresponding to different particular 
cases of this method. The particular function create a list, and in this 
list, I have a function which can modify other elements of the list for 
updating purposes.
See the example below:

particular1=function()
{
obj=list()
obj$names=c("obj$c1","obj$c2")
obj$c1=8
obj$update=function(obj) {obj$c1=15; print(obj$c1)}
general(obj)
}

general=function(obj){
obj$c2<-10
print(obj$c1*obj$c2)
upd=eval(parse(text=deparse(obj$update)))
upd(obj)
print(obj$c1*obj$c2)
}

particular1()

 > particular1()
[1] 80
[1] 15
[1] 80

I would like to have: 80, 15, 150. But, the function obj$update change the 
value locally and do not change the "global" value of obj$c1.
I think it is a problem related environment of the function (I have read 
the previous discussion on this list "do.call and environment") but can not 
find how to solve it.

Hope that somebody can help me,
Sincerely.


Stéphane DRAY
-------------------------------------------------------------------------------------------------- 

Département des Sciences Biologiques
Université de Montréal, C.P. 6128, succursale centre-ville
Montréal, Québec H3C 3J7, Canada

Tel : 514 343 6111 poste 1233
E-mail : stephane.dray at umontreal.ca
-------------------------------------------------------------------------------------------------- 

Web                                          http://www.steph280.freesurf.fr/




More information about the R-help mailing list