Skip to content

scoping error in xy.coords (PR#932)

4 messages · Kjetil Halvorsen, Thomas Lumley, Peter Dalgaard

#
Hola!
... expected output, correct, but when called from inside lowess:
Error in xy.coords(x, y) : x and y lengths differ
debugging in: xy.coords(x, y)

... long listing deleted

    if (is.language(x)) {
        if (inherits(x, "formula") && length(x) == 3) {
            ylab <- deparse(x[[2]])
            xlab <- deparse(x[[3]])
            y <- eval(x[[2]], parent.frame())
            x <- eval(x[[3]], parent.frame())
        }
        else stop("invalid first argument")
    }
    else
... deleted
debug: if (inherits(x, "formula") && length(x) == 3) {
    ylab <- deparse(x[[2]])
    xlab <- deparse(x[[3]])
    y <- eval(x[[2]], parent.frame())
    x <- eval(x[[3]], parent.frame())
} else stop("invalid first argument")
Browse[1]> n
debug: ylab <- deparse(x[[2]])
Browse[1]> n
debug: xlab <- deparse(x[[3]])
Browse[1]> n
debug: y <- eval(x[[2]], parent.frame())
Browse[1]> n
debug: x <- eval(x[[3]], parent.frame())
Browse[1]> n
debug: if (length(x) != length(y)) {
    if (recycle) {
        if ((nx <- length(x)) < (ny <- length(y))) 
            x <- rep(x, length = ny)
        else y <- rep(y, length = nx)
    }
    else stop("x and y lengths differ")
}
Browse[1]> x
y ~ x
Browse[1]> y
NULL

x and y have their original values from the call, the evaluation 
inside is.formula .... have failed.

Browse[1]> c
Error in xy.coords(x, y) : x and y lengths differ



Kjetil Halvorsen

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Fri, 4 May 2001 kjetilh@umsanet.edu.bo wrote:

            
If you don't call them x and y it works
Error in xy.coords(x, y) : x and y lengths differ
$x
 [1]  1  2  3  4  5  6  7  8  9 10

$y
 [1]  1  2  3  4  5  6  7  8  9 10

The problem is that the formula is evaluated in the parent frame, where y
evaluates to NULL, and x evaluates to y~x, rather than in the global
environment where they would both evaluate to vectors.

In order to handle formulas correctly I think xy.coords needs to use the
same sort of indirect evaluation as model.frame: eg
   xy <-substitute(xy.coords(X,Y),list(X=substitute(x),Y=substitute(y)))
   xy<-eval(xy,parent.frame())
works as a replacement for the first two lines of lowess().  I hadn't
realised that xy.coords() was supposed to handle formulas, and since it
doesn't have a data= argument it can't do it very well.

This seems a bit drastic, given the number of times xy.coords is used.
Perhaps there's a simpler solution.


	-thomas

Thomas Lumley			Asst. Professor, Biostatistics
tlumley@u.washington.edu	University of Washington, Seattle
^^^^^^^^^^^^^^^^^^^^^^^^
 NOTE NEW EMAIL ADDRESS

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Thomas Lumley <tlumley@u.washington.edu> writes:
Perhaps use the new formula environments in xy.coords? Seems to work


        if (inherits(x, "formula") && length(x) == 3) {
            ylab <- deparse(x[[2]])
            xlab <- deparse(x[[3]])
            y <- eval(x[[2]], environment(x), parent.frame())
            x <- eval(x[[3]], environment(x), parent.frame())
	}
#
On 4 May 2001, Peter Dalgaard BSA wrote:
Yes, much better.

	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._