Re: [R] How to share variables

From: Sergio Martino <>
Date: Tue 22 Aug 2006 - 19:45:01 EST


Thanks again. I hope not to waste to much of your time.

I delete some lines of your answer

> Each time myfun is run a new environment is created to hold
> its local variables. The parent of that environment is e in
> this example by construction. So e and the environment that
> is temporarily created to hold myfun's variables are distinct.

This means that the enviroment is duplicated, ie it is present twince in memory?
I must keep some big variables and it will be a waste of memory; moreover if I update a value it will be lost.

> > If I can use inside myfun the variable as e$dat (without changing the
> > enviroment (no environment(myfun) <- e statement)) than it will be ok.
> Yes you can. You can either make sure that e is visible to myfun
> via normal scoping rules or pass it explicitly:
> e <- new.env()
> e$dat <- 1:3
> myfun <- function(x) sum(x + e$dat)
> myfun(10)

It solves the problem.
A small drawback is that I need to modify the name of each occurrence of the variable.

> # or passing e explicitly
> myfun2 <- function(x, e) sum(x + e$dat)
> myfun2(10, e)

Any overhead in passing the environment? Is it a pointer?

Sergio mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue Aug 22 19:47:57 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 23 Aug 2006 - 00:23:43 EST.

Mailing list information is available at Please read the posting guide before posting to the list.