MichaelChirico / r-bugs

A ⚠️read-only⚠️mirror of https://bugs.r-project.org/
20 stars 0 forks source link

[BUGZILLA #1861] update() can not find objects #1453

Open MichaelChirico opened 4 years ago

MichaelChirico commented 4 years ago

From: yzhou@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::> 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(∼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'.


METADATA

MichaelChirico commented 4 years ago

From: Peter Dalgaard BSA <p.dalgaard@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>> yzhou@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::> 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,∼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
<CENSORING FROM DETECTED PHONE NUMBER ONWARDS; SEE BUGZILLA>


METADATA

MichaelChirico commented 4 years ago

From: Yi-Xiong Zhou <yzhou@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>> Thanks Peter. This does work for formula. However, it failed to a function call. Here is another test script:

################# begin test ############### fun1 <- function() { x <- matrix(rnorm(500), 20,25) oo <- fun3(x) print("step 1") update(oo) print("first update success.") fun2(oo) }

fun2 <- function(gg) { update(gg) print("second update success.") }

fun3 <- function(aa) { oo <- list(x=aa, call=match.call()) oo }

fun1() ############### end test ############

The error message is

[1] "step 1" [1] "first update success." Error in fun3(aa = x) : Object "x" not found

Yi-Xiong

-----Original Message----- From: Peter Dalgaard BSA [mailto:p.dalgaard@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>] Sent: Thursday, August 01, 2002 10:27 AM To: Yi-Xiong Zhou Cc: r-devel@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>; R-bugs@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::> Subject: Re: update() can not find objects (PR#1861)

yzhou@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::> 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,∼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
<CENSORING FROM DETECTED PHONE NUMBER ONWARDS; SEE BUGZILLA>


METADATA

MichaelChirico commented 4 years ago

From: Thomas Lumley <tlumley@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>> On Thu, 1 Aug 2002 yzhou@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::> wrote:

``` 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, ideally it should search the objects in the environment where the model was defined. There's no guarantee that this is a grandparent, and grandparents shouldn't be searched otherwise. It might be tricky to get this to work. What isn't clear to me is whether the current environment should be searched before the environment where the model was defined or afterwards. -thomas ----------- #### METADATA - Comment author - Jitterbug compatibility account - Timestamp - 2002-08-02 00:36:51 UTC
MichaelChirico commented 4 years ago

From: Thomas Lumley <tlumley@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>> On Thu, 1 Aug 2002, Yi-Xiong Zhou wrote:

Thanks Peter. This does work for formula. However, it failed to a function
call. Here is another test script:

################# begin test ###############
fun1 <- function() {
x <- matrix(rnorm(500), 20,25)
oo <- fun3(x)
print("step 1")
update(oo)
print("first update success.")
fun2(oo)
}

fun2 <- function(gg) {
update(gg)
print("second update success.")
}

fun3 <- function(aa) {
oo <- list(x=aa, call=match.call())
oo
}

fun1()
############### end test ############

The error message is

[1] "step 1"
[1] "first update success."
Error in fun3(aa = x) : Object "x" not found

Yes, but this isn't supposed to work, and can't.

In the first place update() is supposed to work on models, not on arbitrary objects.

In the second place, the object you are returning contains no information about where it was created, so it's not possible for update to work out the correct environment. In this case the correct environment happens to be parent.frame(2), but there's no reason why this should be true in general.

When the object is a model with a formula it does carry information about where it was defined, so update() can look there. Your first example should have worked, and the fact that it didn't is a bug (though arguably just a wishlist bug). This new example shouldn't work.

-thomas

METADATA

MichaelChirico commented 4 years ago

From: Yi-Xiong Zhou <yzhou@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>> Could an object carry the information about where it was created? Could that be on the wish list?

Yi-Xiong

-----Original Message----- From: Thomas Lumley [mailto:tlumley@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>] Sent: Thursday, August 01, 2002 2:14 PM To: Yi-Xiong Zhou Cc: 'Peter Dalgaard BSA'; r-devel@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>; R-bugs@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::> Subject: RE: update() can not find objects (PR#1861)

On Thu, 1 Aug 2002, Yi-Xiong Zhou wrote:

Thanks Peter. This does work for formula. However, it failed to a function
call. Here is another test script:

################# begin test ###############
fun1 <- function() {
x <- matrix(rnorm(500), 20,25)
oo <- fun3(x)
print("step 1")
update(oo)
print("first update success.")
fun2(oo)
}

fun2 <- function(gg) {
update(gg)
print("second update success.")
}

fun3 <- function(aa) {
oo <- list(x=aa, call=match.call())
oo
}

fun1()
############### end test ############

The error message is

[1] "step 1"
[1] "first update success."
Error in fun3(aa = x) : Object "x" not found

Yes, but this isn't supposed to work, and can't.

In the first place update() is supposed to work on models, not on arbitrary objects.

In the second place, the object you are returning contains no information about where it was created, so it's not possible for update to work out the correct environment. In this case the correct environment happens to be parent.frame(2), but there's no reason why this should be true in general.

When the object is a model with a formula it does carry information about where it was defined, so update() can look there. Your first example should have worked, and the fact that it didn't is a bug (though arguably just a wishlist bug). This new example shouldn't work.

-thomas

METADATA

MichaelChirico commented 4 years ago

NOTES: The problem is actually deeper than this.

Sometime update() wants to evaluate arguments in the environment where the model was defined, as here.

Sometimes it wants to use the current environment, eg this snippet from MASS ph.fun <- function(data, i) { d <- data d$calls <- d$fitted + d$res[i] coef(update(fit, data=d)) }


METADATA

MichaelChirico commented 4 years ago

Audit (from Jitterbug): Thu Aug 1 23:00:21 2002 thomas moved from incoming to Models Fri Aug 2 00:01:23 2002 thomas changed notes Fri Aug 2 00:01:23 2002 thomas foobar


METADATA