Debian Codegraph @

R/mtable-format-delim.R

raw
1 hdxp0 <- function(x,span) {
2 y <- matrix(rep("",span),nrow=1,ncol=span)
3 y[,1] <- x
4 y
5 }
6 hdxp1 <- function(x) {
7 span <- attr(x,"span")
8 y <- matrix(rep("",span),nrow=1,ncol=span)
9 y[,1] <- x
10 y
11 }
12 hdxp <- function(x)do.call(cbind,lapply(x,hdxp1))
13
14 ldxp1 <- function(x) {
15 span <- attr(x,"span")
16 y <- matrix(rep("",span),nrow=span,ncol=1)
17 y[1,] <- x
18 y
19 }
20 ldxp <- function(x)do.call(rbind,lapply(x,ldxp1))
21
22
23 mtable_format_delim <- function(x,
24 colsep="\t",
25 rowsep="\n",
26 interaction.sep = " x ",
27 ...)
28 pf_mtable_format_delim(preformat_mtable(x),
29 colsep=colsep,
30 rowsep=rowsep,
31 interaction.sep = interaction.sep,
32 ...)
33
34 pf_mtable_format_delim <- function(x,
35 colsep="\t",
36 rowsep="\n",
37 interaction.sep = " x ",
38 show.parmtypes = nrow(x$parmtab) > 1,
39 ...
40 ){
41
42 pt <- x$parmtab
43 sst <- x$summary.stats
44 sh <- x$sect.headers
45 leaders <- x$leaders
46 headers <- x$headers
47 eq.headers <- x$eq.headers
48
49 res <- NULL
50
51 l.headers <- length(headers)
52 l.leaders <- length(leaders)
53
54 has.eq.headers <- length(eq.headers) > 0
55
56 for(j in 1:ncol(pt)){
57
58 name.j <- colnames(pt)[j]
59 pt.j <- pt[,j]
60 l.pt.j <- length(pt.j)
61
62 ncol.j <- unique(sapply(pt.j,ncol))
63 stopifnot(length(ncol.j)==1)
64
65 for(i in 1:l.pt.j){
66 pt.ij <- pt.j[[i]]
67 if(show.parmtypes){
68 pt.ij <- rbind(" ",pt.ij)
69 }
70 pt.j[[i]] <- pt.ij
71 }
72
73 pt.j <- do.call(rbind,pt.j)
74
75 if(has.eq.headers){
76 eq.header.j <- eq.headers[[name.j]]
77 n.eq.j <- length(eq.header.j)
78 eq.span <- ncol(pt.j)/n.eq.j
79 tmp <- matrix("",ncol=n.eq.j,nrow=eq.span)
80 tmp[1,] <- eq.header.j
81 eq.header.j <- as.vector(tmp)
82 pt.j <- rbind(eq.header.j,pt.j)
83 }
84
85 if(length(sst)){
86 sst.j <- sst[[j]]
87 sst.j <- colexpand(sst.j,ncol.j)
88 pt.j <- rbind(pt.j,sst.j)
89 }
90
91 res <- cbind(res,pt.j)
92 }
93
94 if(l.headers){
95 for(k in 1:l.headers){
96 headers.k <- headers[[k]]
97 headers.k <- lapply(headers.k,hdxp1)
98 headers.k <- do.call(cbind,headers.k)
99 headers[[k]] <- headers.k
100 }
101 headers <- do.call(rbind,headers)
102 res <- rbind(headers,res)
103 }
104
105
106 if(l.leaders){
107 lh <- l.headers + has.eq.headers
108 if(lh)
109 leaders <- c(rep(list(list(structure("",span=1))),lh),
110 leaders)
111 leaders <- lapply(leaders,ldxp)
112 if(show.parmtypes){
113 parmtypes <- rownames(x$parmtab)
114 for(p in parmtypes){
115 leaders[[p]] <- rbind(p,leaders[[p]])
116 }
117 }
118 leaders <- do.call(rbind,leaders)
119
120 res <- cbind(leaders,res)
121 }
122
123 res <- apply(res,1,paste,collapse=colsep)
124 res <- paste0(res,rowsep)
125 return(res)
126 }