From p.dalgaard@biostat.ku.dk  Thu Aug  1 18:26:34 2002
From: p.dalgaard@biostat.ku.dk (Peter Dalgaard BSA)
Date: 01 Aug 2002 19:26:34 +0200
Subject: update() can not find objects (PR#1861)
In-Reply-To: <200208011702.TAA28218@pubhealth.ku.dk>
References: <200208011702.TAA28218@pubhealth.ku.dk>
Message-ID: <x2ptx232yd.fsf@biostat.ku.dk>

yzhou@arcturusag.com writes:

> Full_Name: Yi-Xiong Zhou
> Version: 1.5.1
> OS: win2000pro
> Submission from: (NULL) (64.169.249.42)
> 
> 
> Update() can not find objects when it is used in a function, which is in turn
> being called by another function. Here is a R script to show the problem:
> 
> ######## begin of the test script ##########
> fun1 <- function() {
>    x <- matrix(rnorm(500), 20,25)
>    y <- rnorm(20)
>    oo <- lm(y~x)
>    print("step 1")
>    update(oo)
>    print("first update success.")
>    fun2(oo)
> }
> 
> fun2 <- function(gg) {
>    update(gg)
>    print("second update success.")
> }
> 
> fun1()
> ########### end of the test script #############
> 
> Here is the result of running this script:
> 
> [1] "step 1"
> [1] "first update success."
> Error in eval(expr, envir, enclos) : Object "y" not found
> 
> Ideally, update should first search the objects in its environment first, then
> its parent's, and grand parent's environments ... Right now, it only searchs its
> own environment and the global environment, skipping its parents'. 

No, that's not what you should expect in a language with lexical scoping. 


However, there might still be a bug, since update with a non-missing
formula argument would extract the formula environment from the
formula stored in the lm object, but that doesn't happen if it is
missing. Modifying fun2 to

function(gg) {
   update(gg,formula(gg))
   print("second update success.")
}

or even

function(gg) {
   update(gg,y~x)
   print("second update success.")
}

does allow your example to run, which is somewhat unexpected...

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


update() can not find objects (PR#1861)
Peter Dalgaard BSA