The R Project SVN R-packages

Rev

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

Rev 4529 Rev 4549
Line 221... Line 221...
221
 
221
 
222
### --- show() method ---
222
### --- show() method ---
223
 
223
 
224
## FIXME(?) -- ``merge this'' (at least ``synchronize'') with
224
## FIXME(?) -- ``merge this'' (at least ``synchronize'') with
225
## - - -   prMatrix() from ./Auxiliaries.R
225
## - - -   prMatrix() from ./Auxiliaries.R
226
prSpMatrix <- function(x, digits = getOption("digits"),
-
 
227
                       maxp = getOption("max.print"), zero.print = ".",
-
 
228
                       col.names = FALSE, note.dropping.colnames = TRUE,
-
 
229
                       col.trailer = '', align = c("fancy", "right"))
-
 
230
## FIXME: prTriang() in ./Auxiliaries.R  should also get  align = "fancy"
226
## FIXME: prTriang() in ./Auxiliaries.R  should also get  align = "fancy"
-
 
227
## --> help for this is currently (rudimentary) in ../man/sparseMatrix-class.Rd
-
 
228
printSpMatrix <- function(x, digits = getOption("digits"),
-
 
229
		       maxp = getOption("max.print"), zero.print = ".",
-
 
230
		       col.names, note.dropping.colnames = TRUE,
-
 
231
		       col.trailer = '', align = c("fancy", "right"))
231
{
232
{
232
    cl <- getClassDef(class(x))
233
    cl <- getClassDef(class(x))
233
    stopifnot(extends(cl, "sparseMatrix"))
234
    stopifnot(extends(cl, "sparseMatrix"))
234
    d <- dim(x)
235
    d <- dim(x)
235
    if(prod(d) > maxp) { # "Large" => will be "cut"
236
    if(prod(d) > maxp) { # "Large" => will be "cut"
Line 237... Line 238...
237
        nr <- maxp %/% d[2]
238
        nr <- maxp %/% d[2]
238
	m <- as(x[1:max(1, nr), ,drop=FALSE], "Matrix")
239
	m <- as(x[1:max(1, nr), ,drop=FALSE], "Matrix")
239
    } else {
240
    } else {
240
        m <- as(x, "matrix")
241
        m <- as(x, "matrix")
241
    }
242
    }
-
 
243
    dn <- dimnames(m) ## will be === dimnames(cx)
242
    logi <- extends(cl,"lsparseMatrix") || extends(cl,"nsparseMatrix")
244
    logi <- extends(cl,"lsparseMatrix") || extends(cl,"nsparseMatrix")
243
    if(logi)
245
    if(logi)
244
	cx <- array("N", # or as.character(NA),
246
	cx <- array("N", dim(m), dimnames=dn)
245
		   dim(m), dimnames=dimnames(m))
-
 
246
    else { ## numeric (or --not yet-- complex):
247
    else { ## numeric (or --not yet-- complex):
247
	cx <- apply(m, 2, format)
248
	cx <- apply(m, 2, format)
248
	if(is.null(dim(cx))) {# e.g. in	1 x 1 case
249
	if(is.null(dim(cx))) {# e.g. in	1 x 1 case
249
	    dim(cx) <- dim(m)
250
	    dim(cx) <- dim(m)
250
	    dimnames(cx) <- dimnames(m)
251
	    dimnames(cx) <- dn
251
	}
252
	}
252
    }
253
    }
253
    if(!col.names)
254
    if (missing(col.names))
-
 
255
        col.names <- {
-
 
256
            if(!is.null(cc <- getOption("sparse.colnames")))
-
 
257
                cc
-
 
258
            else if(is.null(dn[[2]]))
-
 
259
                FALSE
-
 
260
            else { # has column names == dn[[2]]
-
 
261
                ncol(x) < 10
-
 
262
            }
-
 
263
        }
-
 
264
    if(identical(col.names, FALSE))
254
        cx <- emptyColnames(cx, msg.if.not.empty = note.dropping.colnames)
265
        cx <- emptyColnames(cx, msg.if.not.empty = note.dropping.colnames)
-
 
266
    else if(is.character(col.names)) {
-
 
267
        stopifnot(length(col.names) == 1)
-
 
268
        cn <- col.names
-
 
269
        switch(substr(cn, 1,3),
-
 
270
               "abb" = {
-
 
271
                   iarg <- as.integer(sub("^[^0-9]*", '', cn))
-
 
272
                   colnames(cx) <- abbreviate(colnames(cx), minlength = iarg)
-
 
273
               },
-
 
274
               "sub" = {
-
 
275
                   iarg <- as.integer(sub("^[^0-9]*", '', cn))
-
 
276
                   colnames(cx) <- substr(colnames(cx), 1, iarg)
-
 
277
               },
-
 
278
               stop("invalid 'col.names' string: ", cn))
-
 
279
    }
-
 
280
    ## else: nothing to do for col.names == TRUE
255
    if(is.logical(zero.print))
281
    if(is.logical(zero.print))
256
	zero.print <- if(zero.print) "0" else " "
282
	zero.print <- if(zero.print) "0" else " "
257
    if(logi) {
283
    if(logi) {
258
	cx[!m] <- zero.print
284
	cx[!m] <- zero.print
259
	cx[m] <- "|"
285
	cx[m] <- "|"
Line 297... Line 323...
297
    ## right = TRUE : cheap attempt to get better "." alignment
323
    ## right = TRUE : cheap attempt to get better "." alignment
298
    print(cx, quote = FALSE, right = TRUE, max = maxp)
324
    print(cx, quote = FALSE, right = TRUE, max = maxp)
299
    invisible(x)
325
    invisible(x)
300
}
326
}
301
 
327
 
302
setMethod("print", signature(x = "sparseMatrix"), prSpMatrix)
328
setMethod("print", signature(x = "sparseMatrix"), printSpMatrix)
303
 
329
 
304
setMethod("show", signature(object = "sparseMatrix"),
330
setMethod("show", signature(object = "sparseMatrix"),
305
   function(object) {
331
   function(object) {
306
       d <- dim(object)
332
       d <- dim(object)
307
       cl <- class(object)
333
       cl <- class(object)
308
       cat(sprintf('%d x %d sparse Matrix of class "%s"\n', d[1], d[2], cl))
334
       cat(sprintf('%d x %d sparse Matrix of class "%s"\n', d[1], d[2], cl))
309
       maxp <- getOption("max.print")
335
       maxp <- getOption("max.print")
310
       if(prod(d) <= maxp)
336
       if(prod(d) <= maxp)
311
	   prSpMatrix(object, maxp = maxp)
337
	   printSpMatrix(object, maxp = maxp)
312
       else { ## d[1] > maxp / d[2] >= nr : -- this needs [,] working:
338
       else { ## d[1] > maxp / d[2] >= nr : -- this needs [,] working:
313
 
339
 
314
	   nR <- d[1] # nrow
340
	   nR <- d[1] # nrow
315
           useW <- getOption("width") - (format.info(nR)[1] + 3+1)
341
           useW <- getOption("width") - (format.info(nR)[1] + 3+1)
316
           ##                           space for "[<last>,] "
342
           ##                           space for "[<last>,] "
Line 325... Line 351...
325
           suppRows <- (nr < nR)
351
           suppRows <- (nr < nR)
326
           if(suppRows) {
352
           if(suppRows) {
327
	       if(suppCols)
353
	       if(suppCols)
328
		   object <- object[ , 1:nc, drop = FALSE]
354
		   object <- object[ , 1:nc, drop = FALSE]
329
	       n2 <- ceiling(nr / 2)
355
	       n2 <- ceiling(nr / 2)
330
	       prSpMatrix(object[seq_len(min(nR, max(1, n2))), , drop=FALSE],
356
	       printSpMatrix(object[seq_len(min(nR, max(1, n2))), , drop=FALSE],
331
			  col.trailer = col.trailer)
357
                             col.trailer = col.trailer)
332
	       cat("\n ..............................",
358
	       cat("\n ..............................",
333
		   "\n ..........suppressing rows in show(); maybe adjust 'options(max.print= *)'",
359
		   "\n ..........suppressing rows in show(); maybe adjust 'options(max.print= *)'",
334
		   "\n ..............................\n\n", sep='')
360
		   "\n ..............................\n\n", sep='')
335
	       ## tail() automagically uses "[..,]" rownames:
361
	       ## tail() automagically uses "[..,]" rownames:
336
	       prSpMatrix(tail(object, max(1, nr-n2)),
362
	       printSpMatrix(tail(object, max(1, nr-n2)),
337
			  col.trailer = col.trailer)
363
                             col.trailer = col.trailer)
338
	   }
364
	   }
339
	   else if(suppCols) {
365
	   else if(suppCols) {
340
	       prSpMatrix(object[ , 1:nc , drop = FALSE],
366
	       printSpMatrix(object[ , 1:nc , drop = FALSE],
341
			  col.trailer = col.trailer)
367
                             col.trailer = col.trailer)
342
 
368
 
343
	       cat("\n .....suppressing columns in show(); maybe adjust 'options(max.print= *)'",
369
	       cat("\n .....suppressing columns in show(); maybe adjust 'options(max.print= *)'",
344
		   "\n ..............................\n", sep='')
370
		   "\n ..............................\n", sep='')
345
	   }
371
	   }
346
           else stop("logic programming error in prSpMatrix(), please report")
372
           else stop("logic programming error in printSpMatrix(), please report")
347
 
373
 
348
           invisible(object)
374
           invisible(object)
349
       }
375
       }
350
   })
376
   })
351
 
377