From maechler at stat.math.ethz.ch  Tue Jul 28 11:59:36 2009
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Tue, 28 Jul 2009 11:59:36 +0200
Subject: [Rd] bug in approx crashes R
In-Reply-To: <22D850BC39A25742977325ADDE208E770271F720B4@chiexchange02.w2k.jumptrading.com>
References: <22D850BC39A25742977325ADDE208E770271F720AD@chiexchange02.w2k.jumptrading.com>
	<77EB52C6DD32BA4D87471DCD70C8D700018AEA43@NA-PA-VBE03.na.tibco.com>
	<22D850BC39A25742977325ADDE208E770271F720B4@chiexchange02.w2k.jumptrading.com>
Message-ID: <19054.52232.465181.458748@lynne.math.ethz.ch>

>>>>> Vadim Ogranovich <vogranovich at jumptrading.com>
>>>>>     on Mon, 27 Jul 2009 12:47:47 -0500 writes:

    > Thank you Bill.

Indeed, thank you, Bill Dunlap!

    > The original motivation for my
    > experiments with setting yleft to NULL was to see if I
    > could get more flexibility that that allowed by the
    > 'rule' argument. To recall:

    >     rule: an integer describing how interpolation is to
    > take place outside the interval ['min(x)', 'max(x)']. If
    > 'rule' is '1' then 'NA's are returned for such points
    > and if it is '2', the value at the closest data extreme
    > is used.

    > What I wanted is to interpolate at the left end, but not
    > at the right end. Still don't know how to do that.

Really?
If you quickly browse the code, you see that's it's simply a
matter of correctly setting  yleft  and  yright.

But in order to help future users, and since it's so trivial,
I will change approxfun() {my favorite} and approx() to also
accept
	rule = c(1,2)
or	rule = c(2,1)


    > I agree that having a clear error message when yleft,
    > yright, and f are set to non-scalars is better than
    > silently returning NA.

yes, I agree too.

---
Martin Maechler, ETH Zurich


    > Thanks,
    > Vadim


    > -----Original Message-----
    > From: William Dunlap [mailto:wdunlap at tibco.com]
    > Sent: Monday, July 27, 2009 12:14 PM
    > To: Vadim Ogranovich; r-devel at r-project.org
    > Subject: RE: [Rd] 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

    >> -----Original Message-----
    >> From: r-devel-bounces at r-project.org
    >> [mailto:r-devel-bounces at r-project.org] On Behalf Of Vadim Ogranovich
    >> Sent: Tuesday, July 21, 2009 12:24 PM
    >> To: 'r-devel at r-project.org'
    >> Subject: [Rd] bug in approx crashes R
    >> 
    >> Dear R-devel,
    >> 
    >> The following line crashes R
    >> > approx(1, 1, 0, method='const', rule=2, f=0, yleft=NULL,
    >> ties='ordered')$y
    >> 
    >> Process R:2 exited abnormally with code 5 at Tue Jul 21 14:18:09 2009
    >> 
    >> 
    >> > version
    >> _
    >> platform       i386-pc-mingw32
    >> arch           i386
    >> os             mingw32
    >> system         i386, mingw32
    >> status
    >> major          2
    >> minor          9.1
    >> year           2009
    >> month          06
    >> day            26
    >> svn rev        48839
    >> language       R
    >> version.string R version 2.9.1 (2009-06-26)
    >> 
    >> Thanks,
    >> Vadim
    >> 
    >> ______________________________________________
    >> R-devel at r-project.org mailing list
    >> https://stat.ethz.ch/mailman/listinfo/r-devel
    >>



[Rd] bug in approx crashes R
Martin Maechler