[R] Assigning variables into an environment.

Rolf Turner r.turner at auckland.ac.nz
Thu Dec 10 04:43:08 CET 2009


I am working with a somewhat complicated structure in which
I need to deal with a function that takes ``basic'' arguments
and also depends on a number of parameters which change depending
on circumstances.

I thought that a sexy way of dealing with this would be to assign
the parameters as objects in the environment of the function in
question.

The following toy example gives a bit of the flavour of what I
am trying to do:

foo <- function(x,zeta) {
for(nm in names(zeta)) assign(nm,zeta[nm],envir=environment(bar))
bar(x)
}

bar <- function(x) {
alpha + beta*exp(gamma*x)
}

v <- c(alpha=2,beta=3,gamma=-4)

ls()
[1] "bar" "foo" "v"

foo(0.1,v)
   alpha
4.01096

2+3*exp(-4*0.1)
[1] 4.01096      # Check; yes it's working; but ...

ls()
[1] "alpha" "bar"   "beta"  "foo"   "gamma" "v"

The parameters got assigned in the global environment (as well as in
the environment of bar()? Or instead of?).

I didn't want that to happen.

Questions:

(a) What did I do wrong?
(b) What am I not understanding about environments?
(c) How can I get the parameters to be assigned in the environment of  
bar()
     and ***NOT*** in the global environment?
(d) Is it time to go to the pub yet?

[Please don't make suggestions about doing it all some other way, e.g.
using the ``...'' argument facility.  I know there are other ways to
attack my problem (but they may not be so efficacious in the context
of my real --- as opposed to toy --- example).  I want to try to get
the environment idea to work, and I want to understand more about
environments and how they work.]

Thanks for any insights.

	cheers,

		Rolf Turner

P. S. Are there any articles about environments (in the sense used  
above)
out there in the literature?  I searched the contents of the R  
Journal/R News
but turned up nothing on environments.

		R. T.

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}




More information about the R-help mailing list