[Rd] empty array indexing with dimnames -> erroneous error (PR#2507)

maechler@stat.math.ethz.ch maechler@stat.math.ethz.ch
Thu Jan 30 18:14:17 2003


This is a bug that will not often trigger, and only happens for arrays
(but not matrices) ending up with 0 dimensions:

The following code also shows two 'buglets' (the first even for
matrix indexing) where "drop = TRUE/FALSE" is not correctly obeyed.
One can argue that since all these arrays have length zero, it
shouldn't matter what dimensions they get.
[But this shouldn't distract from the real bug giving errors
 where it shouldn't!]


##-- this kind of indexing works with matrices (with dimnames):
m <- cbind(x = 1, y = 1:3)
str(m[1,  0])# drops dimension
str(m[1:2,0])# doesn't drop  <<< ? buglet
str(m[1,  0, drop=FALSE])
str(m[1:2,0, drop=FALSE])


## these apply (not too relevant here)
stopifnot(identical(m[1,         0],
                    m[1, integer(0)]))
stopifnot(identical(m[1,         0, drop=FALSE],
                    m[1, integer(0), drop=FALSE]))

###--- but it fails with arrays ---

dn <- list(LETTERS[1:2], letters[1:3], paste("t",1:4,sep=""))
A. <- array(1:24, dim = 2:4) # no dimnames
Ad <- A.; dimnames(Ad) <- dn # with dimnames

## works `ok', if no dimnames --- not always obeying "drop=" --> 2nd buglet!
for(O in list(0, integer(0))) {
  str(A.[1, O, 2 ]) # int(0)
  str(A.[O,-1, O ])# int[0, 1:2, 0]
  str(A.[O, O,2:3])# int[0, 0, 1:2]

  ## with drop = FALSE:
  str(A.[1, O, 2 , drop=FALSE])# int[1, 0, 1]
  str(A.[O,-1, O , drop=FALSE])# int[0, 1:2, 0]
  str(A.[O, O,2:3, drop=FALSE])# int[0, 0, 1:2]
  cat("---\n")
}

##-- identical with `Ad' instead of `A.', i.e., *with* dimnames:
##--> All 6 statements fail :

O <- 0##__for(O in list(0, integer(0))) {

  try(Ad[1, O, 2 ]) # int(0)
  try(Ad[O,-1, O ])# int[0, 1:2, 0]
  try(Ad[O, O,2:3])# int[0, 0, 1:2]

  ## with drop = FALSE:
  try(Ad[1, O, 2 , drop=FALSE])# int[1, 0, 1]
  try(Ad[O,-1, O , drop=FALSE])# int[0, 1:2, 0]
  try(Ad[O, O,2:3, drop=FALSE])# int[0, 0, 1:2]
  cat("---\n")
##__}