Skip to content

Commit 0839ad0

Browse files
author
maechler
committed
better print(<array>, max = <small>) incl "omitted" messages
git-svn-id: https://svn.r-project.org/R/trunk@87761 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 753d735 commit 0839ad0

File tree

4 files changed

+83
-31
lines changed

4 files changed

+83
-31
lines changed

doc/NEWS.Rd

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@
283283
(Some packages ignore these settings in their \command{configure}
284284
script or when compiling in sub-directories of \file{src}, as
285285
will those using a \file{src/Makefile}.)
286-
286+
287287
\item Source installs now report the package version in the log.
288288
289289
\item There is preliminary support for C++26 with GCC \eqn{>=} 14, Apple
@@ -600,6 +600,11 @@
600600
\item \code{quantile()}'s default method gets an option \code{fuzz}
601601
to protect against floating point inaccuracy before rounding, thus
602602
fixing \PR{15811} and, \I{en passant}, \PR{17683}.
603+
604+
\item Printing arrays now also omits columns, rows and slices when
605+
desirable according to option \code{max.print}, or argument
606+
\code{max}, respectively, addressing most of the remaining part of
607+
\PR{15027}, thanks to \I{Sherry Zhang}'s patch.
603608
}
604609
}
605610
}

src/main/printarray.c

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* R : A Computer Language for Statistical Data Analysis
3-
* Copyright (C) 2000--2024 The R Core Team
3+
* Copyright (C) 2000--2025 The R Core Team
44
* Copyright (C) 2001--2012 The R Foundation
55
* Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka
66
*
@@ -409,7 +409,7 @@ void printArray(SEXP x, SEXP dim, int quote, int right, SEXP dimnames)
409409
else { /* ndim >= 3 */
410410
SEXP dn, dnn, dn0, dn1;
411411
const int *dims = INTEGER_RO(dim);
412-
int i, j, nb, nb_pr, nr_last,
412+
int i, j, nb, nb_pr, ne_last, nc_last, nr_last,
413413
nr = dims[0], nc = dims[1],
414414
b = nr * nc;
415415
Rboolean max_reached, has_dimnames = (dimnames != R_NilValue),
@@ -443,16 +443,21 @@ void printArray(SEXP x, SEXP dim, int quote, int right, SEXP dimnames)
443443
nb_pr = ceil_DIV(R_print.max, b);
444444
/* for the last, (nb_pr)th matrix slice, use only nr_last rows;
445445
* using floor(), not ceil(), since 'nc' could be huge: */
446-
nr_last = (R_print.max - b * (nb_pr - 1)) / nc;
447-
if(nr_last == 0) { nb_pr--; nr_last = nr; }
446+
ne_last = R_print.max - b * (nb_pr - 1);
447+
nc_last = (ne_last < nc) ? 0: nc;
448+
nr_last = (ne_last < nc) ? 0: ne_last / nc;
449+
if(nr_last == 0) { nb_pr--; nc_last = nc; nr_last = nr;}
448450
} else {
449451
nb_pr = (nb > 0) ? nb : 1; // do print *something* when dim = c(a,b,0)
452+
ne_last = b;
453+
nc_last = nc;
450454
nr_last = nr;
451455
}
452456
for (i = 0; i < nb_pr; i++) {
453457
Rboolean do_ij = nb > 0,
454458
i_last = (i == nb_pr - 1); /* for the last slice */
455-
int use_nr = i_last ? nr_last : nr;
459+
int use_nc = i_last ? nc_last : nc,
460+
use_nr = i_last ? nr_last : nr;
456461
if(do_ij) {
457462
int k = 1;
458463
Rprintf(", ");
@@ -478,36 +483,44 @@ void printArray(SEXP x, SEXP dim, int quote, int right, SEXP dimnames)
478483
}
479484
switch (TYPEOF(x)) {
480485
case LGLSXP:
481-
printLogicalMatrix(x, i * b, use_nr, nr, nc, dn0, dn1, rn, cn, do_ij);
486+
printLogicalMatrix(x, i * b, use_nr, nr, use_nc, dn0, dn1, rn, cn, do_ij);
482487
break;
483488
case INTSXP:
484-
printIntegerMatrix(x, i * b, use_nr, nr, nc, dn0, dn1, rn, cn, do_ij);
489+
printIntegerMatrix(x, i * b, use_nr, nr, use_nc, dn0, dn1, rn, cn, do_ij);
485490
break;
486491
case REALSXP:
487-
printRealMatrix (x, i * b, use_nr, nr, nc, dn0, dn1, rn, cn, do_ij);
492+
printRealMatrix (x, i * b, use_nr, nr, use_nc, dn0, dn1, rn, cn, do_ij);
488493
break;
489494
case CPLXSXP:
490-
printComplexMatrix(x, i * b, use_nr, nr, nc, dn0, dn1, rn, cn, do_ij);
495+
printComplexMatrix(x, i * b, use_nr, nr, use_nc, dn0, dn1, rn, cn, do_ij);
491496
break;
492497
case STRSXP:
493498
if (quote) quote = '"';
494-
printStringMatrix (x, i * b, use_nr, nr, nc,
499+
printStringMatrix (x, i * b, use_nr, nr, use_nc,
495500
quote, right, dn0, dn1, rn, cn, do_ij);
496501
break;
497502
case RAWSXP:
498-
printRawMatrix (x, i * b, use_nr, nr, nc, dn0, dn1, rn, cn, do_ij);
503+
printRawMatrix (x, i * b, use_nr, nr, use_nc, dn0, dn1, rn, cn, do_ij);
499504
break;
500505
}
501506
Rprintf("\n");
502507
}
503508

504-
if(max_reached && nb_pr < nb) {
509+
if (max_reached) {
505510
Rprintf(" [ reached 'max' / getOption(\"max.print\") -- omitted");
506-
if(nr_last < nr) Rprintf(" %d row(s) and", nr - nr_last);
507-
Rprintf(" %d matrix slice(s) ]\n", nb - nb_pr);
511+
if (nb_pr < nb)
512+
Rprintf(ngettext(" %d slice", " %d slices", nb - nb_pr), nb - nb_pr);
513+
else if (nb_pr == nb) {
514+
if(nr_last < nr) Rprintf(ngettext(" %d row", " %d rows", nr - nr_last), nr - nr_last);
515+
if(nc_last < nc) Rprintf(ngettext(" %d column", " %d columns", nc - nc_last), nc - nc_last);
516+
/* == MM: replace the above with
517+
if((nr -= nr_last) > 0) Rprintf(ngettext(" %d row", " %d rows", nr), nr);
518+
if((nc -= nc_last) > 0) Rprintf(ngettext(" %d column", " %d columns", nc), nc);
519+
*/
520+
}
521+
Rprintf(" ] \n");
508522
}
509523
}
510524
UNPROTECT(nprotect);
511525
vmaxset(vmax);
512526
}
513-

tests/print-tests.R

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,14 @@ print(matrix(nrow = 100, ncol = 100), max = 40) # omitting rows and columns
346346
print(matrix(nrow = 10, ncol = 4), max = 3) # (ditto)
347347
print(matrix(nrow = 0, ncol = 4), max = 3) # omitting 1 column
348348
print(matrix(nrow = 10, ncol = 2), max = 5) # omitting rows
349-
print(matrix(nrow = 1, ncol = 6), max = 5) # omitting cols, at least one row prints
349+
print(matrix(nrow = 1, ncol = 6), max = 5) # omitting 1 col, at least one row prints
350350
## ----- "higher" arrays ("rank >= 3"): --------
351-
## FIXME: in R 4.4.0 there should be a warning for omitted rows
352-
print(array(dim = c(2, 2, 1)), max = 2)
353-
## FIXME: this does not print anything but it should show
354-
## at least one element according to the logic of max.print
355-
print(array(dim = c(2, 2, 1)), max = 1)
351+
print(array(dim = c(2, 2, 2)), max = 5) # omit 1 slice
352+
print(array(dim = c(2, 2, 2)), max = 6) # omit 1 row
353+
print(array(dim = c(2, 2, 2)), max = 7) # omit 1 row
354+
#
355+
print(array(dim = c(2, 2, 1)), max = 2) # omit 1 row
356+
print(array(dim = c(2, 2, 1)), max = 1) # omit the only slice
356357

357358

358359

tests/print-tests.Rout.save

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
R Under development (unstable) (2024-09-20 r87182) -- "Unsuffered Consequences"
3-
Copyright (C) 2024 The R Foundation for Statistical Computing
2+
R Under development (unstable) (2025-02-20 r87760) -- "Unsuffered Consequences"
3+
Copyright (C) 2025 The R Foundation for Statistical Computing
44
Platform: x86_64-pc-linux-gnu
55

66
R is free software and comes with ABSOLUTELY NO WARRANTY.
@@ -969,22 +969,55 @@ NULL
969969
[1,] NA NA
970970
[2,] NA NA
971971
[ reached 'max' / getOption("max.print") -- omitted 8 rows ]
972-
> print(matrix(nrow = 1, ncol = 6), max = 5) # omitting cols, at least one row prints
972+
> print(matrix(nrow = 1, ncol = 6), max = 5) # omitting 1 col, at least one row prints
973973
[,1] [,2] [,3] [,4] [,5]
974974
[1,] NA NA NA NA NA
975975
[ reached 'max' / getOption("max.print") -- omitted 1 column ]
976976
> ## ----- "higher" arrays ("rank >= 3"): --------
977-
> ## FIXME: in R 4.4.0 there should be a warning for omitted rows
978-
> print(array(dim = c(2, 2, 1)), max = 2)
977+
> print(array(dim = c(2, 2, 2)), max = 5) # omit 1 slice
979978
, , 1
980979

981980
[,1] [,2]
982981
[1,] NA NA
982+
[2,] NA NA
983983

984-
> ## FIXME: this does not print anything but it should show
985-
> ## at least one element according to the logic of max.print
986-
> print(array(dim = c(2, 2, 1)), max = 1)
987-
[ reached 'max' / getOption("max.print") -- omitted 1 matrix slice(s) ]
984+
[ reached 'max' / getOption("max.print") -- omitted 1 slice ]
985+
> print(array(dim = c(2, 2, 2)), max = 6) # omit 1 row
986+
, , 1
987+
988+
[,1] [,2]
989+
[1,] NA NA
990+
[2,] NA NA
991+
992+
, , 2
993+
994+
[,1] [,2]
995+
[1,] NA NA
996+
997+
[ reached 'max' / getOption("max.print") -- omitted 1 row ]
998+
> print(array(dim = c(2, 2, 2)), max = 7) # omit 1 row
999+
, , 1
1000+
1001+
[,1] [,2]
1002+
[1,] NA NA
1003+
[2,] NA NA
1004+
1005+
, , 2
1006+
1007+
[,1] [,2]
1008+
[1,] NA NA
1009+
1010+
[ reached 'max' / getOption("max.print") -- omitted 1 row ]
1011+
> #
1012+
> print(array(dim = c(2, 2, 1)), max = 2) # omit 1 row
1013+
, , 1
1014+
1015+
[,1] [,2]
1016+
[1,] NA NA
1017+
1018+
[ reached 'max' / getOption("max.print") -- omitted 1 row ]
1019+
> print(array(dim = c(2, 2, 1)), max = 1) # omit the only slice
1020+
[ reached 'max' / getOption("max.print") -- omitted 1 slice ]
9881021
>
9891022
>
9901023
>

0 commit comments

Comments
 (0)