Skip to content
Prev 38745 / 63424 Next

environment question

On Dec 26, 2010, at 22:30 , Paul Johnson wrote:

            
Yes. I'm pretty sure that the net effect is the same as redefining the three functions inside the current function. I.e.

g <- function(fee){fee+fie(fum)}
f <- function(foo){
  environment(g) <- environment()
  fum <- 3.14
  g(foo)
}

is equivalent to

g <- function(fee){fee+fie(fum)}
f <- function(foo){
  g <- function(fee){fee+fie(fum)}
  fum <- 3.14
  g(foo)
}

since a local copy must be created before the environment of g can be changed.
First of all, those are arguments, not options. Arguments can be optional (when there is a default, mostly) but that is something else. Options are set with, say, options(width=60).
Well, only if the call is always EM(betas, constraints, ....). They could on occasion be matched to something else.
You are "allowed" to alter anything that you can find. Sometimes it is just a very bad idea, and/or bad programming style...

The superassignment operator "<<-" was explicitly designed to allow modification of objects in the lexical scope of a function, so at least in some cases, it must be considered good style to use it (examples can be found in the paper by Ihaka and Gentleman on lexical scope, 1996 IIRC). However, some care must be taken; in particular, if you don't make sure that the object already exists in the appropriate environment, another object of the same name might get clobbered, e.g. in the global environment.

Best,
-pd

(& thanks for that KU t-shirt, by the way!)