The R Project SVN R

Rev

Rev 33359 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 33359 Rev 34864
Line 1... Line 1...
1
/*
1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
2
 *  R : A Computer Language for Statistical Data Analysis
3
 *  Copyright (C) 1995, 1996	Robert Gentleman and Ross Ihaka
3
 *  Copyright (C) 1995, 1996	Robert Gentleman and Ross Ihaka
4
 *  Copyright (C) 2000--2002	The R Development Core Team.
4
 *  Copyright (C) 2000--2005	The R Development Core Team.
5
 *
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
9
 *  (at your option) any later version.
Line 25... Line 25...
25
 *			 and the Encode.. utils.
25
 *			 and the Encode.. utils.
26
 *
26
 *
27
 *  See ./format.c	 for the  format_FOO_  functions used below.
27
 *  See ./format.c	 for the  format_FOO_  functions used below.
28
 */
28
 */
29
 
29
 
30
/* <UTF8> char here is handled as a whole */
30
/* <UTF8> char here is handled as a whole, 
-
 
31
   but lengths were used as display widths */
31
 
32
 
32
#ifdef HAVE_CONFIG_H
33
#ifdef HAVE_CONFIG_H
33
#include <config.h>
34
#include <config.h>
34
#endif
35
#endif
35
 
36
 
36
#include "Defn.h"
37
#include "Defn.h"
37
#include "Print.h"
38
#include "Print.h"
38
 
39
 
-
 
40
/* We need display width of a string */
-
 
41
int Rstrwid(char *str, int slen, int quote);  /* from printutils.c */
-
 
42
#define strwidth(x) Rstrwid(x, strlen(x), 0)
-
 
43
 
39
static void printLogicalMatrix(SEXP sx, int offset, int r, int c,
44
static void printLogicalMatrix(SEXP sx, int offset, int r, int c,
40
			       SEXP rl, SEXP cl, char *rn, char *cn)
45
			       SEXP rl, SEXP cl, char *rn, char *cn)
41
{
46
{
42
    SEXP sw;
47
    SEXP sw;
43
    int *x, *w;
48
    int *x, *w;
Line 48... Line 53...
48
	formatString(STRING_PTR(rl), r, &rlabw, 0);
53
	formatString(STRING_PTR(rl), r, &rlabw, 0);
49
    else
54
    else
50
	rlabw = IndexWidth(r + 1) + 3;
55
	rlabw = IndexWidth(r + 1) + 3;
51
 
56
 
52
    if (rn) {
57
    if (rn) {
53
	rnw = strlen(rn);
58
	rnw = strwidth(rn);
54
	if ( rnw < rlabw + R_MIN_LBLOFF )
59
	if ( rnw < rlabw + R_MIN_LBLOFF )
55
	    lbloff = R_MIN_LBLOFF;
60
	    lbloff = R_MIN_LBLOFF;
56
	else
61
	else
57
	    lbloff = rnw - rlabw;
62
	    lbloff = rnw - rlabw;
58
 
63
 
Line 66... Line 71...
66
    for (j = 0; j < c; j++) {
71
    for (j = 0; j < c; j++) {
67
	formatLogical(&x[j * r], r, &w[j]);
72
	formatLogical(&x[j * r], r, &w[j]);
68
	if (!isNull(cl)) {
73
	if (!isNull(cl)) {
69
	    if(STRING_ELT(cl, j) == NA_STRING) 
74
	    if(STRING_ELT(cl, j) == NA_STRING) 
70
		clabw = R_print.na_width_noquote;
75
		clabw = R_print.na_width_noquote;
71
	    else clabw = strlen(CHAR(STRING_ELT(cl, j)));
76
	    else clabw = strwidth(CHAR(STRING_ELT(cl, j)));
72
	} else
77
	} else
73
	    clabw = IndexWidth(j + 1) + 3;
78
	    clabw = IndexWidth(j + 1) + 3;
74
	if (w[j] < clabw)
79
	if (w[j] < clabw)
75
	    w[j] = clabw;
80
	    w[j] = clabw;
76
	w[j] += R_print.gap;
81
	w[j] += R_print.gap;
Line 124... Line 129...
124
	formatString(STRING_PTR(rl), r, &rlabw, 0);
129
	formatString(STRING_PTR(rl), r, &rlabw, 0);
125
    else
130
    else
126
	rlabw = IndexWidth(r + 1) + 3;
131
	rlabw = IndexWidth(r + 1) + 3;
127
 
132
 
128
    if (rn) {
133
    if (rn) {
129
	rnw = strlen(rn);
134
	rnw = strwidth(rn);
130
	if ( rnw < rlabw + R_MIN_LBLOFF )
135
	if ( rnw < rlabw + R_MIN_LBLOFF )
131
	    lbloff = R_MIN_LBLOFF;
136
	    lbloff = R_MIN_LBLOFF;
132
	else
137
	else
133
	    lbloff = rnw - rlabw;
138
	    lbloff = rnw - rlabw;
134
 
139
 
Line 141... Line 146...
141
    for (j = 0; j < c; j++) {
146
    for (j = 0; j < c; j++) {
142
	formatInteger(&x[j * r], r, &w[j]);
147
	formatInteger(&x[j * r], r, &w[j]);
143
	if (!isNull(cl)) {
148
	if (!isNull(cl)) {
144
	    if(STRING_ELT(cl, j) == NA_STRING) 
149
	    if(STRING_ELT(cl, j) == NA_STRING) 
145
		clabw = R_print.na_width_noquote;
150
		clabw = R_print.na_width_noquote;
146
	    else clabw = strlen(CHAR(STRING_ELT(cl, j)));
151
	    else clabw = strwidth(CHAR(STRING_ELT(cl, j)));
147
	} else
152
	} else
148
	    clabw = IndexWidth(j + 1) + 3;
153
	    clabw = IndexWidth(j + 1) + 3;
149
	if (w[j] < clabw)
154
	if (w[j] < clabw)
150
	    w[j] = clabw;
155
	    w[j] = clabw;
151
	w[j] += R_print.gap;
156
	w[j] += R_print.gap;
Line 200... Line 205...
200
	formatString(STRING_PTR(rl), r, &rlabw, 0);
205
	formatString(STRING_PTR(rl), r, &rlabw, 0);
201
    else
206
    else
202
	rlabw = IndexWidth(r + 1) + 3;
207
	rlabw = IndexWidth(r + 1) + 3;
203
 
208
 
204
    if (rn) {
209
    if (rn) {
205
	rnw = strlen(rn);
210
	rnw = strwidth(rn);
206
	if ( rnw < rlabw + R_MIN_LBLOFF )
211
	if ( rnw < rlabw + R_MIN_LBLOFF )
207
	    lbloff = R_MIN_LBLOFF;
212
	    lbloff = R_MIN_LBLOFF;
208
	else
213
	else
209
	    lbloff = rnw - rlabw;
214
	    lbloff = rnw - rlabw;
210
 
215
 
Line 223... Line 228...
223
    for (j = 0; j < c; j++) {
228
    for (j = 0; j < c; j++) {
224
	formatReal(&x[j * r], r, &w[j], &d[j], &e[j], 0);
229
	formatReal(&x[j * r], r, &w[j], &d[j], &e[j], 0);
225
	if (!isNull(cl)) {
230
	if (!isNull(cl)) {
226
	    if(STRING_ELT(cl, j) == NA_STRING) 
231
	    if(STRING_ELT(cl, j) == NA_STRING) 
227
		clabw = R_print.na_width_noquote;
232
		clabw = R_print.na_width_noquote;
228
	    else clabw = strlen(CHAR(STRING_ELT(cl, j)));
233
	    else clabw = strwidth(CHAR(STRING_ELT(cl, j)));
229
	} else
234
	} else
230
	    clabw = IndexWidth(j + 1) + 3;
235
	    clabw = IndexWidth(j + 1) + 3;
231
	if (w[j] < clabw)
236
	if (w[j] < clabw)
232
	    w[j] = clabw;
237
	    w[j] = clabw;
233
	w[j] += R_print.gap;
238
	w[j] += R_print.gap;
Line 259... Line 264...
259
	for (j = jmin; j < jmax ; j++)
264
	for (j = jmin; j < jmax ; j++)
260
	    MatrixColumnLabel(cl, j, w[j]);
265
	    MatrixColumnLabel(cl, j, w[j]);
261
	for (i = 0; i < r; i++) {
266
	for (i = 0; i < r; i++) {
262
	    MatrixRowLabel(rl, i, rlabw, lbloff);
267
	    MatrixRowLabel(rl, i, rlabw, lbloff);
263
	    for (j = jmin; j < jmax; j++) {
268
	    for (j = jmin; j < jmax; j++) {
264
		Rprintf("%s", EncodeReal(x[i + j * r], w[j], d[j], e[j]));
269
		Rprintf("%s", EncodeReal(x[i + j * r], w[j], d[j], e[j], OutDec));
265
	    }
270
	    }
266
	}
271
	}
267
	Rprintf("\n");
272
	Rprintf("\n");
268
	jmin = jmax;
273
	jmin = jmax;
269
    }
274
    }
Line 282... Line 287...
282
	formatString(STRING_PTR(rl), r, &rlabw, 0);
287
	formatString(STRING_PTR(rl), r, &rlabw, 0);
283
    else
288
    else
284
	rlabw = IndexWidth(r + 1) + 3;
289
	rlabw = IndexWidth(r + 1) + 3;
285
 
290
 
286
    if (rn) {
291
    if (rn) {
287
	rnw = strlen(rn);
292
	rnw = strwidth(rn);
288
	if ( rnw < rlabw + R_MIN_LBLOFF )
293
	if ( rnw < rlabw + R_MIN_LBLOFF )
289
	    lbloff = R_MIN_LBLOFF;
294
	    lbloff = R_MIN_LBLOFF;
290
	else
295
	else
291
	    lbloff = rnw - rlabw;
296
	    lbloff = rnw - rlabw;
292
 
297
 
Line 317... Line 322...
317
		      &wr[j], &dr[j], &er[j],
322
		      &wr[j], &dr[j], &er[j],
318
		      &wi[j], &di[j], &ei[j], 0);
323
		      &wi[j], &di[j], &ei[j], 0);
319
	if (!isNull(cl)) {
324
	if (!isNull(cl)) {
320
	    if(STRING_ELT(cl, j) == NA_STRING) 
325
	    if(STRING_ELT(cl, j) == NA_STRING) 
321
		clabw = R_print.na_width_noquote;
326
		clabw = R_print.na_width_noquote;
322
	    else clabw = strlen(CHAR(STRING_ELT(cl, j)));
327
	    else clabw = strwidth(CHAR(STRING_ELT(cl, j)));
323
	} else
328
	} else
324
	    clabw = IndexWidth(j + 1) + 3;
329
	    clabw = IndexWidth(j + 1) + 3;
325
	w[j] = wr[j] + wi[j] + 2;
330
	w[j] = wr[j] + wi[j] + 2;
326
	if (w[j] < clabw)
331
	if (w[j] < clabw)
327
	    w[j] = clabw;
332
	    w[j] = clabw;
Line 356... Line 361...
356
	    MatrixColumnLabel(cl, j, w[j]);
361
	    MatrixColumnLabel(cl, j, w[j]);
357
	for (i = 0; i < r; i++) {
362
	for (i = 0; i < r; i++) {
358
	    MatrixRowLabel(rl, i, rlabw, lbloff);
363
	    MatrixRowLabel(rl, i, rlabw, lbloff);
359
	    for (j = jmin; j < jmax; j++) {
364
	    for (j = jmin; j < jmax; j++) {
360
		if (ISNA(x[i + j * r].r) || ISNA(x[i + j * r].i))
365
		if (ISNA(x[i + j * r].r) || ISNA(x[i + j * r].i))
361
		    Rprintf("%s", EncodeReal(NA_REAL, w[j], 0, 0));
366
		    Rprintf("%s", EncodeReal(NA_REAL, w[j], 0, 0, OutDec));
362
		else
367
		else
363
		    Rprintf("%s",
368
		    Rprintf("%s",
364
			    EncodeComplex(x[i + j * r],
369
			    EncodeComplex(x[i + j * r],
365
					  wr[j] + R_print.gap, dr[j], er[j],
370
					  wr[j] + R_print.gap, dr[j], er[j],
366
					  wi[j], di[j], ei[j]));
371
					  wi[j], di[j], ei[j], OutDec));
367
	    }
372
	    }
368
	}
373
	}
369
	Rprintf("\n");
374
	Rprintf("\n");
370
	jmin = jmax;
375
	jmin = jmax;
371
    }
376
    }
Line 385... Line 390...
385
	formatString(STRING_PTR(rl), r, &rlabw, 0);
390
	formatString(STRING_PTR(rl), r, &rlabw, 0);
386
    else
391
    else
387
	rlabw = IndexWidth(r + 1) + 3;
392
	rlabw = IndexWidth(r + 1) + 3;
388
 
393
 
389
    if (rn) {
394
    if (rn) {
390
	rnw = strlen(rn);
395
	rnw = strwidth(rn);
391
	if ( rnw < rlabw + R_MIN_LBLOFF )
396
	if ( rnw < rlabw + R_MIN_LBLOFF )
392
	    lbloff = R_MIN_LBLOFF;
397
	    lbloff = R_MIN_LBLOFF;
393
	else
398
	else
394
	    lbloff = rnw - rlabw;
399
	    lbloff = rnw - rlabw;
395
 
400
 
Line 402... Line 407...
402
    for (j = 0; j < c; j++) {
407
    for (j = 0; j < c; j++) {
403
	formatString(&x[j * r], r, &w[j], quote);
408
	formatString(&x[j * r], r, &w[j], quote);
404
	if (!isNull(cl)) {
409
	if (!isNull(cl)) {
405
	    if(STRING_ELT(cl, j) == NA_STRING) 
410
	    if(STRING_ELT(cl, j) == NA_STRING) 
406
		clabw = R_print.na_width_noquote;
411
		clabw = R_print.na_width_noquote;
407
	    else clabw = strlen(CHAR(STRING_ELT(cl, j)));
412
	    else clabw = strwidth(CHAR(STRING_ELT(cl, j)));
408
	} else
413
	} else
409
	    clabw = IndexWidth(j + 1) + 3;
414
	    clabw = IndexWidth(j + 1) + 3;
410
	if (w[j] < clabw)
415
	if (w[j] < clabw)
411
	    w[j] = clabw;
416
	    w[j] = clabw;
412
    }
417
    }
Line 452... Line 457...
452
	Rprintf("\n");
457
	Rprintf("\n");
453
	jmin = jmax;
458
	jmin = jmax;
454
    }
459
    }
455
}
460
}
456
 
461
 
-
 
462
static void printRawMatrix(SEXP sx, int offset, int r, int c,
-
 
463
			   SEXP rl, SEXP cl, char *rn, char *cn)
-
 
464
{
-
 
465
    SEXP sw;
-
 
466
    int *w;
-
 
467
    Rbyte *x;
-
 
468
    int width, rlabw, clabw, rnw;
-
 
469
    int i, j, jmin, jmax, lbloff = 0;
-
 
470
 
-
 
471
    if (!isNull(rl))
-
 
472
	formatString(STRING_PTR(rl), r, &rlabw, 0);
-
 
473
    else
-
 
474
	rlabw = IndexWidth(r + 1) + 3;
-
 
475
 
-
 
476
    if (rn) {
-
 
477
	rnw = strwidth(rn);
-
 
478
	if ( rnw < rlabw + R_MIN_LBLOFF )
-
 
479
	    lbloff = R_MIN_LBLOFF;
-
 
480
	else
-
 
481
	    lbloff = rnw - rlabw;
-
 
482
 
-
 
483
	rlabw += lbloff;
-
 
484
    }
-
 
485
 
-
 
486
    sw = allocVector(INTSXP, c);
-
 
487
    x = RAW(sx) + offset;
-
 
488
    w = INTEGER(sw);
-
 
489
    for (j = 0; j < c; j++) {
-
 
490
	formatRaw(&x[j * r], r, &w[j]);
-
 
491
	if (!isNull(cl)) {
-
 
492
	    if(STRING_ELT(cl, j) == NA_STRING) 
-
 
493
		clabw = R_print.na_width_noquote;
-
 
494
	    else clabw = strwidth(CHAR(STRING_ELT(cl, j)));
-
 
495
	} else
-
 
496
	    clabw = IndexWidth(j + 1) + 3;
-
 
497
	if (w[j] < clabw)
-
 
498
	    w[j] = clabw;
-
 
499
	w[j] += R_print.gap;
-
 
500
    }
-
 
501
    jmin = 0;
-
 
502
    jmax = 0;
-
 
503
    if (c == 0) {
-
 
504
	for (i = 0; i < r; i++)
-
 
505
	    MatrixRowLabel(rl, i, rlabw, lbloff);
-
 
506
	Rprintf("\n");
-
 
507
	return;
-
 
508
    }
-
 
509
    while (jmin < c) {
-
 
510
	width = rlabw;
-
 
511
	do {
-
 
512
	    width += w[jmax];
-
 
513
	    jmax++;
-
 
514
	}
-
 
515
	while (jmax < c && width + w[jmax] < R_print.width);
-
 
516
 
-
 
517
	if (cn != NULL)
-
 
518
	    Rprintf("%*s%s\n", rlabw, "", cn);
-
 
519
 
-
 
520
	if (rn != NULL)
-
 
521
	    Rprintf("%*s", -rlabw, rn);
-
 
522
	else
-
 
523
	    Rprintf("%*s", rlabw, "");
-
 
524
 
-
 
525
	for (j = jmin; j < jmax ; j++)
-
 
526
	    MatrixColumnLabel(cl, j, w[j]);
-
 
527
	for (i = 0; i < r; i++) {
-
 
528
	    MatrixRowLabel(rl, i, rlabw, lbloff);
-
 
529
	    for (j = jmin; j < jmax; j++)
-
 
530
		Rprintf("%*s%s", w[j]-2, "", EncodeRaw(x[i + j * r]));
-
 
531
	}
-
 
532
	Rprintf("\n");
-
 
533
	jmin = jmax;
-
 
534
    }
-
 
535
}
-
 
536
 
457
void printMatrix(SEXP x, int offset, SEXP dim, int quote, int right,
537
void printMatrix(SEXP x, int offset, SEXP dim, int quote, int right,
458
		 SEXP rl, SEXP cl, char *rn, char *cn)
538
		 SEXP rl, SEXP cl, char *rn, char *cn)
459
{
539
{
460
    int r, c;
540
    int r, c;
461
 
541
 
Line 485... Line 565...
485
	break;
565
	break;
486
    case STRSXP:
566
    case STRSXP:
487
	if (quote) quote = '"';
567
	if (quote) quote = '"';
488
	printStringMatrix(x, offset, r, c, quote, right, rl, cl, rn, cn);
568
	printStringMatrix(x, offset, r, c, quote, right, rl, cl, rn, cn);
489
	break;
569
	break;
-
 
570
    case RAWSXP:
-
 
571
	printRawMatrix(x, offset, r, c, rl, cl, rn, cn);
-
 
572
	break;
-
 
573
    default:
-
 
574
	UNIMPLEMENTED_TYPE("printMatrix", x);
490
    }
575
    }
491
}
576
}
492
 
577
 
493
static void printArrayGeneral(SEXP x, SEXP dim, int quote, int right, 
578
static void printArrayGeneral(SEXP x, SEXP dim, int quote, int right, 
494
                              SEXP dimnames)
579
                              SEXP dimnames)
Line 565... Line 650...
565
		break;
650
		break;
566
	    case STRSXP:
651
	    case STRSXP:
567
		if (quote) quote = '"';
652
		if (quote) quote = '"';
568
		printStringMatrix(x, i * b, nr, nc, quote, right, dn0, dn1, rn, cn);
653
		printStringMatrix(x, i * b, nr, nc, quote, right, dn0, dn1, rn, cn);
569
		break;
654
		break;
-
 
655
	    case RAWSXP:
-
 
656
		printRawMatrix(x, i * b, nr, nc, dn0, dn1, rn, cn);
-
 
657
		break;
570
	    }
658
	    }
571
	    Rprintf("\n");
659
	    Rprintf("\n");
572
	}
660
	}
573
    }
661
    }
574
}
662
}