Bug 16012 - named dimnames print()ing not ok for 0-column matrix and other arrays
Summary: named dimnames print()ing not ok for 0-column matrix and other arrays
Status: CLOSED FIXED
Alias: None
Product: R
Classification: Unclassified
Component: I/O (show other bugs)
Version: R-devel (trunk)
Hardware: Other All
: P5 minor
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2014-10-08 08:16 UTC by Martin Maechler
Modified: 2015-12-14 13:45 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Maechler 2014-10-08 08:16:52 UTC
We still "forget" that  dimnames(.) can be named in R,
something IIRC Peter invented in the time before R 1.0.0 and we use
"extensively" for producing nicely printing  table(.) results.

However our print function fails to combine this well when a matrix (or an
array) in addition has zero extents.

It seems, the printing is wrong only in the case where

	    tail(dim(.), 1)  ==  0  :


> r <- matrix(,0,4, dimnames=list(Row=NULL, Col=paste0("c",1:4)))

## a matrix with named dimnames and 0 rows -- prints fine:

> r
      Col
Row    c1 c2 c3 c4

## But not so with 0 columns :

> t(r)

  c1
  c2
  c3
  c4
> 
## --------- Even worse for non-matrix arrays:

> A <- array(dim=c(3,2,0), dimnames=list(D1=c("a","b","c"), D2=c("X","Y"), D3=NULL))
> str(A)
 logi[1:3, 1:2, 0 ] 
 - attr(*, "dimnames")=List of 3
  ..$ D1: chr [1:3] "a" "b" "c"
  ..$ D2: chr [1:2] "X" "Y"
  ..$ D3: NULL

> A

## i.e., when the last dimension is 0, it does not print *anything* !

## whereas it prints fine in cases when another dimension is zero :

> aperm(A, c(3,1:2))
, , D2 = X

      D1
D3     a b c

, , D2 = Y

      D1
D3     a b c

> 

## a 1d array prints identical to a vector .. which we can accept as this
## is also the case for other 1d arrays

> a1 <- array(dim=0, dimnames=list(D=NULL))
> a1
logical(0)
> (array(1:2, dim=2))
[1] 1 2
> (a2 <- array(1:2, dim=2, dimnames=list(c("x","y"))))
x y 
1 2 
> (a2. <- array(1:2, dim=2, dimnames=list(D = c("x","y"))))
D
x y 
1 2 

## where the last one *does* print the names(dimnames(.))
Comment 1 Martin Maechler 2014-10-08 19:38:24 UTC
fixing it myself ...  currently planned to also go into R 3.1.2
Comment 2 Martin Maechler 2014-10-10 07:31:13 UTC
currently only in R-devel ... current fix as a "Request for Comments" (RFC).

to be backported to R pre-3.1.2  soon as that will go into freeze.