Skip to content
Prev 33381 / 63424 Next

bug in approx crashes R

The C code called by approx (via .C, not .Call), following the help
file,
assumes that yleft and yright are scalars but NULL is not scalar.
The following change would let your example work (returning NA)

--- R/approx.R  (revision 48911)
+++ R/approx.R  (working copy)
@@ -61,8 +61,8 @@
     }
     y <- .C("R_approx", as.double(x), as.double(y), as.integer(nx),
            xout = as.double(xout), as.integer(length(xout)),
-           as.integer(method), as.double(yleft), as.double(yright),
-           as.double(f), NAOK = TRUE, PACKAGE = "stats")$xout
+           as.integer(method), as.double(yleft)[1],
as.double(yright)[1],
+           as.double(f)[1], NAOK = TRUE, PACKAGE = "stats")$xout
     list(x = xout, y = y)
 }

but I think it would be better to get an error message that yleft,
yright, and f are expected to be scalar:

--- R/approx.R  (revision 48911)
+++ R/approx.R  (working copy)
@@ -59,6 +59,7 @@
            stop("'approx' requires n >= 1")
        xout <- seq.int(x[1L], x[nx], length.out = n)
     }
+    stopifnot(length(yleft)==1, length(yright)==1, length(f)==1)
     y <- .C("R_approx", as.double(x), as.double(y), as.integer(nx),
            xout = as.double(xout), as.integer(length(xout)),
            as.integer(method), as.double(yleft), as.double(yright),


Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com