Debian Codegraph @

R/mtable-tools-format.R

raw
1
2
3 centerAt <- function(x,at=getOption("OutDec"),integers=c("dot","right","left"),skip=0){
4 has.dot <- setdiff(grep(at,x,fixed=TRUE),skip)
5 if(!any(has.dot>0)) return(x)
6 x <- trimws(x)
7 is.int <- setdiff(seq(x),union(has.dot,skip))
8 splitted <- strsplit(x[has.dot],at,fixed=TRUE)
9 left <- sapply(splitted,function(x)x[1])
10 maxleft <- max(nchar(left))
11 right <- sapply(splitted,function(x)paste(x[-1],collapse="."))
12 maxright <- max(nchar(right))
13 maxcentered <- maxleft+maxright+1
14
15 if(any(is.int>0)){
16 integers <- match.arg(integers)
17 if(integers=="right"){
18 left <- format(left,justify="right",width=maxleft)
19 right <- format(right,justify="left",width=maxright)
20 fintegers <- format(x[is.int],
21 justify="right",
22 width=maxcentered)
23 }
24 if(integers=="left"){
25 left <- format(left,justify="right",width=maxleft)
26 right <- format(right,justify="left",width=maxright)
27 fintegers <- format(x[is.int],
28 justify="left",
29 width=maxcentered)
30 }
31 if(integers=="dot"){
32 maxleft <- max(maxleft,max(nchar(as.character(x[is.int]))))
33 left <- format(left,justify="right",width=maxleft)
34 right <- format(right,justify="left",width=maxright)
35 fintegers <- format(x[is.int],
36 justify="right",
37 width=maxleft)
38 fintegers <- paste(fintegers,format(" ",width=maxright))
39 }
40 centered <- paste(left,right,sep=".")
41 maxcentered <- max(nchar(centered))
42 x[has.dot] <- centered
43 x[is.int] <- fintegers
44 } else {
45 left <- format(left,justify="right",width=maxleft)
46 right <- format(right,justify="left",width=maxright)
47 centered <- paste(left,right,sep=".")
48 x[has.dot] <- centered
49 }
50 if(any(as.logical(skip)))
51 x[skip] <- format(x[skip],width=maxcentered,justify="centre")
52
53 return(x)
54 }
55
56 coefxpand <- function(x,names){
57 if(length(x)){
58 d <- dx <- dim(x)
59 dd <- ddx <- dimnames(x)
60 ddNULL <- sapply(dd,is.null)
61 d[3] <- length(names)
62 dd[[3]] <- names
63 dd_ <- dd
64 dd[ddNULL] <- lapply(d[ddNULL],seq,from=1)
65 ddx[ddNULL] <- dd[ddNULL]
66 res <- array("",dim=d,dimnames=dd)
67 call.arg <- list(res)
68 call.arg <- c(call.arg,ddx)
69 call.arg <- c(call.arg,list(value=as.vector(x)))
70 res <- do.call("[<-",call.arg)
71 dimnames(res) <- dd_
72 res
73 }
74 else if(length(dim(x))){
75 d <- dim(x)
76 dd <- dimnames(x)
77 d[3] <- length(names)
78 dd[[3]] <- names
79 array("",dim=d,dimnames=dd)
80 } else {
81 r <- length(names)
82 array("",dim=c(r,1,1),
83 dimnames=list(names,NULL,NULL))
84 }
85 }
86
87 smryxpand <- function(x,names){
88
89 res <- matrix(rep("",length(names)),ncol=1)
90 rownames(res) <- names
91 if(length(x)) {
92 nms.x <- rownames(x)
93 res[nms.x,] <- x
94 }
95 return(res)
96 }