[Rd] segments() with zero-length arguments (PR#11192)

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Thu Apr 17 12:16:24 CEST 2008


ripley at stats.ox.ac.uk wrote:
> I think we should allow only all-zero arguments (at present any 
> zero-length argument is an error), as per the R-level proposed code.
>
> arrows() and rect() share the code so it is much cleaner to do this 
> internally.
>
>   
There are precedents for not requiring all-or-none zero-length args. In
arithmetic, "recycling" treats the case of one zero length item as if
all had length zero

> 2+numeric(0)
numeric(0)

cbind() and rbind() are a bit anomalous in that they just throw away the
offending item:

> cbind(a=1:2,b=numeric(0),c=3:4)
     a c
[1,] 1 3
[2,] 2 4

Only the former variant would make sense for segments(), but segments()
does recycle, unlike lines() which complains if vectors are of different
length.

I think there are cases where you might want it to just do nothing
rather than warn and do nothing or cause an error. Consider rug()-like code:
segments(x, ytop, x, ybot) with an empty x. (I realize that the real
rug() uses Axis(), but the point remains.)

> Done for R-devel.
>
> On Thu, 17 Apr 2008, rmh at temple.edu wrote:
>
>   
>> Uwe Ligges suggested I post this on R-bugs as a wishlist item with a
>> proposed patch.  R considers zero-length arguments to segments() to be
>> an error.  I would like R to allow this and to return without an
>> error.  It occurs naturally in settings like
>>
>> valid <- c(FALSE, FALSE, FALSE)
>> segments(x0[valid], y0[valid], x1[valid], y1[valid])
>>
>> For what it may be worth, S-Plus does not consider zero-length
>> arguments to segments() be an error.
>>
>>
>> plot(1:10)
>> segments(1,1,10,10,col='green')
>> segments(numeric(0), numeric(0), numeric(0), numeric(0), col='green')
>> Error in segments(x0, y0, x1, y1, col = col, lty = lty, lwd = lwd, ...) :
>>        invalid first argument
>>
>>
>> segments.proposal <-
>>  function (x0, y0, x1, y1, col = par("fg"), lty = par("lty"),
>>            lwd = par("lwd"), ...) {
>>    if (length(x0)==0 && length(y0)==0 && length(x1)==0 && length(y1)==0)
>>      return(invisible(NULL))
>>    .Internal(segments(x0, y0, x1, y1, col = col, lty = lty, lwd = lwd, ...))
>> }
>>
>> segments.proposal(numeric(0), numeric(0), numeric(0), numeric(0), col='green')
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>     
>
>   


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907



More information about the R-devel mailing list