Hi, Consider the code : g <- function(){ print(.x) .x <- 3 } f <- function(){ environment(g) <- environment() .x <- 2 g() .x } > f() [1] 2 [1] 2 I would like f() to return 3. How can I do that ? Am I completely out of place ? Doing that, I want to avoid to pass .x as a parameter in f, because in real life .x is pretty big and g() is called over and over in a loop. Thanks Romain -- visit the R Graph Gallery : http://addictedtor.free.fr/graphiques mixmod 1.7 is released : http://www-math.univ-fcomte.fr/mixmod/index.php +---------------------------------------------------------------+ | Romain FRANCOIS - http://francoisromain.free.fr | | Doctorant INRIA Futurs / EDF | +---------------------------------------------------------------+
Romain Francois <francoisromain at free.fr> writes:> Hi, > > Consider the code : > > g <- function(){ > print(.x) > .x <- 3 > } > > f <- function(){ > environment(g) <- environment() > .x <- 2 > g() > .x > } > > > f() > [1] 2 > [1] 2 > > > I would like f() to return 3. How can I do that ? Am I completely out of > place ? > Doing that, I want to avoid to pass .x as a parameter in f, because in > real life .x is pretty big and g() is called over and over in a loop.If you want to assign into the environment of g, you'll need <<- , otherwise you assign to a local variable. Another possibility involves assign(..., parent.frame()) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On Wed, 26 Apr 2006, Romain Francois wrote:> Hi, > > Consider the code : > > g <- function(){ > print(.x) > .x <- 3 > } > > f <- function(){ > environment(g) <- environment() > .x <- 2 > g() > .x > } > > > f() > [1] 2 > [1] 2 > > > I would like f() to return 3. How can I do that ? Am I completely out of > place ? > Doing that, I want to avoid to pass .x as a parameter in f, because in > real life .x is pretty big and g() is called over and over in a loop. >As long as .x doesn't get modified it probably won't be copied, so this is unlikely to make any difference to memory use even if you work out how to do it. -thomas Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle