diff --git a/src/main/printarray.c b/src/main/printarray.c index b159c50fdab..f51d4c013e8 100644 --- a/src/main/printarray.c +++ b/src/main/printarray.c @@ -1,3 +1,4 @@ + /* * R : A Computer Language for Statistical Data Analysis * Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka @@ -348,35 +349,65 @@ void printMatrix(SEXP x, int offset, SEXP dim, int quote, int right, if(c > 0 && R_print.max / c < r) /* avoid integer overflow */ /* using floor(), not ceil(), since 'c' could be huge: */ r_pr = R_print.max / c; + int c_pr = c; + if (c > R_print.max) { + c_pr = R_print.max; + if (r > 0) { + r_pr = 1; + } + } switch (TYPEOF(x)) { case LGLSXP: - printLogicalMatrix(x, offset, r_pr, r, c, rl, cl, rn, cn, TRUE); + printLogicalMatrix(x, offset, r_pr, r, c_pr, rl, cl, rn, cn, TRUE); break; case INTSXP: - printIntegerMatrix(x, offset, r_pr, r, c, rl, cl, rn, cn, TRUE); + printIntegerMatrix(x, offset, r_pr, r, c_pr, rl, cl, rn, cn, TRUE); break; case REALSXP: - printRealMatrix (x, offset, r_pr, r, c, rl, cl, rn, cn, TRUE); + printRealMatrix (x, offset, r_pr, r, c_pr, rl, cl, rn, cn, TRUE); break; case CPLXSXP: - printComplexMatrix(x, offset, r_pr, r, c, rl, cl, rn, cn, TRUE); + printComplexMatrix(x, offset, r_pr, r, c_pr, rl, cl, rn, cn, TRUE); break; case STRSXP: if (quote) quote = '"'; - printStringMatrix (x, offset, r_pr, r, c, quote, right, rl, cl, rn, cn, TRUE); + printStringMatrix (x, offset, r_pr, r, c_pr, quote, right, rl, cl, rn, cn, TRUE); break; case RAWSXP: - printRawMatrix (x, offset, r_pr, r, c, rl, cl, rn, cn, TRUE); + printRawMatrix (x, offset, r_pr, r, c_pr, rl, cl, rn, cn, TRUE); break; default: UNIMPLEMENTED_TYPE("printMatrix", x); } #ifdef ENABLE_NLS - if(r_pr < r) // number of formats must be consistent here - Rprintf(ngettext(" [ reached getOption(\"max.print\") -- omitted %d row ]\n", - " [ reached getOption(\"max.print\") -- omitted %d rows ]\n", - r - r_pr), - r - r_pr); + if (r == 1 && (c - c_pr) > 0) { + Rprintf(ngettext(" [ reached getOption(\"max.print\") -- omitted %d column ]\n", + " [ reached getOption(\"max.print\") -- omitted %d columns ]\n", + c - c_pr), + c - c_pr); + } + if (r_pr < r) { // number of formats must be consistent here + if ((c - c_pr) <= 0) { + Rprintf(ngettext(" [ reached getOption(\"max.print\") -- omitted %d row ]\n", + " [ reached getOption(\"max.print\") -- omitted %d rows ]\n", + r - r_pr), + r - r_pr); + } + else { + if ((c-c_pr) == 1) { + Rprintf(ngettext(" [ reached getOption(\"max.print\") -- omitted %d row and 1 column ]\n", + " [ reached getOption(\"max.print\") -- omitted %d rows and 1 column ]\n", + r - r_pr), + r - r_pr); + } + else { + Rprintf(ngettext(" [ reached getOption(\"max.print\") -- omitted %d row and %d columns ]\n", + " [ reached getOption(\"max.print\") -- omitted %d rows and %d columns ]\n", + r - r_pr), + r - r_pr, c-c_pr); + } + } + } #else if(r_pr < r) Rprintf(" [ reached getOption(\"max.print\") -- omitted %d rows ]\n", @@ -504,4 +535,3 @@ void printArray(SEXP x, SEXP dim, int quote, int right, SEXP dimnames) UNPROTECT(nprotect); vmaxset(vmax); } - diff --git a/tests/print-tests.R b/tests/print-tests.R index bcafe7e8976..3afd2d81b86 100644 --- a/tests/print-tests.R +++ b/tests/print-tests.R @@ -339,5 +339,22 @@ as.complex(1:10) as.raw(1:10) options(o) +## max.print and max +## bug 15027 https://bugs.r-project.org/show_bug.cgi?id=15027 +## whenever the columns are larger than max.print, no values inside the matrix are displayed +print(matrix(nrow = 100, ncol = 4), max = 5) ## works +## fix: ommiting rows **and** columns and warning printed +## case: omiting rows and columns +print(matrix(nrow = 10, ncol = 4), max = 3) +## case: omitting rows +print(matrix(nrow = 10, ncol = 2), max = 5) +## case: omitting cols, at least one row prints +print(matrix(nrow = 1, ncol = 6), max = 5) +## in R 4.4.0 there should be a warning for ommited rows (NEW BUG) +print(array(dim = c(2, 4, 1)), max = 3) +## this does not print anything but it should show +## at least one element according to the logic of max.print +print(array(dim = c(2, 2, 1)), max = 1) + ## Cleanup rm(print.foo, obj, a, b, c, d, o) diff --git a/tests/print-tests.Rout.save b/tests/print-tests.Rout.save index 34d914e6ca1..f28b3f8c7dd 100644 --- a/tests/print-tests.Rout.save +++ b/tests/print-tests.Rout.save @@ -941,6 +941,38 @@ NULL [ reached getOption("max.print") -- omitted 5 entries ] > options(o) > +> ## max.print and max +> ## bug 15027 https://bugs.r-project.org/show_bug.cgi?id=15027 +> ## whenever the columns are larger than max.print, no values inside the matrix are displayed +> print(matrix(nrow = 100, ncol = 4), max = 5) ## works + [,1] [,2] [,3] [,4] + [1,] NA NA NA NA + [ reached getOption("max.print") -- omitted 99 rows ] +> ## fix: ommiting rows **and** columns and warning printed +> ## case: omiting rows and columns +> print(matrix(nrow = 10, ncol = 4), max = 3) + [,1] [,2] [,3] + [1,] NA NA NA + [ reached getOption("max.print") -- omitted 9 rows and 1 column ] +> ## case: omitting rows +> print(matrix(nrow = 10, ncol = 2), max = 5) + [,1] [,2] + [1,] NA NA + [2,] NA NA + [ reached getOption("max.print") -- omitted 8 rows ] +> ## case: omitting cols, at least one row prints +> print(matrix(nrow = 1, ncol = 6), max = 5) + [,1] [,2] [,3] [,4] [,5] +[1,] NA NA NA NA NA + [ reached getOption("max.print") -- omitted 1 column ] +> ## in R 4.4.0 there should be a warning for ommited rows (NEW BUG) +> print(array(dim = c(2, 4, 1)), max = 3) + [ reached getOption("max.print") -- omitted 1 matrix slice(s) ] +> ## this does not print anything but it should show +> ## at least one element according to the logic of max.print +> print(array(dim = c(2, 2, 1)), max = 1) + [ reached getOption("max.print") -- omitted 1 matrix slice(s) ] +> > ## Cleanup > rm(print.foo, obj, a, b, c, d, o) >