From saikat at stat.wisc.edu  Thu Aug  7 11:24:17 2003
From: saikat at stat.wisc.edu (Saikat DebRoy)
Date: Thu Aug  7 16:28:01 2003
Subject: [Rd] model.frame() call from inside a function (PR#3671)
Message-ID: <6645C002-C8E3-11D7-AF59-0003931A3C9E@stat.wisc.edu>

That attachment disappeared from the previous mail. Here is the patch I 
mentioned.

diff -Naur src/library/nls/R/nls.R /tmp/R/nls.R
--- src/library/nls/R/nls.R	Mon Jul 28 11:11:06 2003
+++ /tmp/R/nls.R	Thu Aug  7 10:04:49 2003
@@ -38,7 +38,7 @@

  nlsModel.plinear <- function( form, data, start ) {
      thisEnv <- environment()
-    env <- new.env()
+    env <- new.env(parent=environment(form))
      for( i in names( data ) ) {
          assign( i, data[[i]], envir = env )
      }
@@ -216,7 +216,7 @@
             },
             predict = function(newdata = list(), qr = FALSE)
             {
-               Env <- new.env()
+               Env <- new.env(parent=environment(form))
                 for (i in objects(envir = env)) {
                     assign(i, get(i, envir = env), envir = Env)
                 }
@@ -234,7 +234,7 @@

  nlsModel <- function( form, data, start ) {
      thisEnv <- environment()
-    env <- new.env()
+    env <- new.env(parent=environment(form))
      for( i in names( data ) ) {
          assign( i, data[[i]], envir = env )
      }
@@ -374,7 +374,7 @@
             Rmat = function() qr.R( QR ),
             predict = function(newdata = list(), qr = FALSE)
             {
-               Env <- new.env()
+               Env <- new.env(parent=environment(form))
                 for (i in objects(envir = env)) {
                     assign(i, get(i, envir = env), envir = Env)
                 }
@@ -398,7 +398,9 @@
              subset, weights, na.action)
  {
      mf <- match.call()             # for creating the model frame
+    env <- environment(formula)
      formula <- as.formula( formula )
+
      varNames <- all.vars(formula)  # parameter and variable names from 
formula

      ## adjust a one-sided model formula by using 0 as the response
@@ -435,7 +437,7 @@

      mf$formula <-                         # replace RHS by linear 
model formula
        parse( text = paste("~", paste( varNames[varIndex], collapse = 
"+")))[[1]]
-
+    environment(mf$formula) <- env
      mf$start <- mf$control <- mf$algorithm <- mf$trace <- NULL
      mf[[1]] <- as.name("model.frame")
      mf <- as.list(eval(mf, parent.frame()))


[Rd] model.frame() call from inside a function (PR#3671)
Saikat DebRoy