Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix max print bug 15027 #175

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions src/main/printarray.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/*
* R : A Computer Language for Statistical Data Analysis
* Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -504,4 +535,3 @@ void printArray(SEXP x, SEXP dim, int quote, int right, SEXP dimnames)
UNPROTECT(nprotect);
vmaxset(vmax);
}

17 changes: 17 additions & 0 deletions tests/print-tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a typo in the issue, this is actually:

print(array(dim= c(2, 2, 1)), max = 2)

## 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)
32 changes: 32 additions & 0 deletions tests/print-tests.Rout.save
Original file line number Diff line number Diff line change
Expand Up @@ -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)
>
Loading