Debian Codegraph @

R/format-html-ftable-matrix.R

raw
1 ftab_mat_html_env <- new.env()
2 ftab_mat_html_env$counter <- 1
3
4 style_ftab_matrix_group_rule <- function(id=NULL,class=NULL,rulewidth=1){
5 class_or_id <- class_id_selector(id=id,class=class)
6 rulewidth <- paste0(rulewidth,"px")
7 selector_tmpl <- "table<<class-or-id>> td:nth-child(n of .header)"
8 selector <- fillin(selector_tmpl,c("class-or-id"=class_or_id))
9 style_tmpl <- paste("<<selector>> {",
10 " border-bottom: <<rulewidth>> solid;",
11 "}",sep="\n")
12 fillin(style_tmpl,c(selector=selector,rulewidth=rulewidth))
13 }
14
15 format_html.ftable_matrix <- function(x,
16 show.titles=TRUE,
17 digits=0,
18 format="f",
19 toprule=2,midrule=1,bottomrule=2,
20 split.dec=TRUE,
21 style=ftable_format_stdstyle,
22 margin="2ex auto",
23 varontop,
24 varinfront,
25 grouprules=1,
26 multi_digits=NULL,
27 ...){
28
29 row.vars <- attr(x,"row.vars")
30 col.vars <- attr(x,"col.vars")
31
32 nms.cv <- names(col.vars)
33 n.col.vars <- length(col.vars)
34
35 N <- nrow(x)
36 M <- ncol(x)
37
38 n <- sapply(x[,1],nrow)
39 m <- sapply(x[1,],ncol)
40
41 l.cv <- sapply(col.vars,length)
42 max.l.cv <- max(l.cv)
43
44 l.rv <- sapply(row.vars,length)
45 max.l.rv <- max(l.rv)
46
47 if(missing(varontop)) varontop <- (max.l.cv <= 1)
48 if(missing(varinfront)) varinfront <- (max.l.rv <= 1)
49
50 if(!missing(multi_digits)){
51 if(is.list(multi_digits)){
52 digits <- multi_digits
53 }
54 else {
55 digits <- rep(list(multi_digits),M)
56 }
57 }
58 else {
59 d <- digits
60 digits <- integer(M)
61 digits[] <- d
62 }
63
64 fo <- format
65 format <- integer(M)
66 format[] <- fo
67
68
69 headers <- mapply(htfm_mkHeader,col.vars,m,
70 MoreArgs=list(max.l.cv,varontop,split.dec),SIMPLIFY=FALSE)
71 leaders <- mapply(htfm_mkLeader,row.vars,n,
72 MoreArgs=list(max.l.rv,varinfront),SIMPLIFY=FALSE)
73
74 body <- array(list(),dim=dim(x))
75
76 mm <- length(headers[[1]])
77 nn <- ncol(leaders[[1]])
78
79 lheaders <- matrix("",nrow=mm,ncol=nn)
80
81 dot_cols <- integer(0)
82 col_sums <- 0
83
84 for(i in 1:N){
85 rv.i <- row.vars[[i]]
86
87 if(l.rv[i] > 1 || (!varinfront && nzchar(names(rv.i)))){
88
89 n.rv.i <- names(rv.i)
90 if(l.rv[i] > 1 && varinfront)
91 n.rv.i[1] <- ""
92
93 if(i == 1){
94 lheaders[mm,] <- n.rv.i
95 }
96 else {
97 tmp.ldr <- leaders[[i]]
98 tmp.ldr <- rbind(html_td(n.rv.i,vectorize=TRUE),tmp.ldr)
99 leaders[[i]] <- tmp.ldr
100 }
101 }
102 if(i < N) {
103 tmp.ldr <- leaders[[i]]
104 leaders[[i]] <- tmp.ldr
105 }
106
107 for(j in 1:M){
108 cv.j <- col.vars[[j]]
109 tmp.bdy <- htfm_mkBody(x[[i,j]],format[[j]],digits[[j]],split.dec)
110 if(i == 1 && split.dec) {
111 nm.cv.j <- names(cv.j)
112 nz.nm.cv.j <- length(nm.cv.j) && all(nzchar(nm.cv.j))
113 m.j <- ncol(tmp.bdy)
114 if((!varontop || max.l.cv > 1) && nz.nm.cv.j) {
115 col_sums <- col_sums + 1
116 # message(sprintf("Incremented col_sums j=%d varontop=%d max.l.cv=%d col_sums=%d",j,varontop,max.l.cv,col_sums))
117 # print(dot_cols)
118 }
119 dot_cols <- c(dot_cols, col_sums + (1:m.j-1)*3 + 2)
120 col_sums <- col_sums + 3*m.j
121 }
122 if(i > 1 && (l.rv[i] > 1 || (!varinfront && nzchar(names(rv.i))))){
123
124 if(split.dec){
125 tmp. <- spltDec(rep("",ncol(tmp.bdy)))
126 tmp.bdy <- rbind(html_td_spltDec(tmp.),tmp.bdy)
127 }
128 else
129 tmp.bdy <- rbind(html_td(rep("",ncol(tmp.bdy)),vectorize=TRUE),tmp.bdy)
130 }
131 if(l.cv[j] > 1 || (!varontop && nzchar(names(cv.j)))){
132
133 tmp.bdy <- cbind(html_td(rep("",nrow(tmp.bdy)),vectorize=TRUE),tmp.bdy)
134 }
135 if(i < N){
136 nnn <- nrow(tmp.bdy)
137 }
138 body[[i,j]] <- tmp.bdy
139 }
140 }
141
142 lheaders <- array(html_td(lheaders,vectorize=TRUE),
143 dim=dim(lheaders))
144
145 leaders <- rbind(lheaders,do.call(rbind,leaders))
146
147 res <- list()
148 for(j in 1:M){
149 res.j <- body[,j]
150 res.j <- do.call(rbind,res.j)
151 res.j <- apply(res.j,1,as.html_group)
152 res[[j]] <- as.html_group(c(headers[[j]],res.j))
153 }
154 res <- do.call(cbind,res)
155 res <- cbind(leaders,res)
156
157 nh <- nrow(lheaders)
158 nl <- ncol(leaders)
159 ntot <- nrow(res)
160
161 res <- apply(res,1,as.html_group)
162
163 res <- html_tr(res,vectorize=TRUE)
164
165 ftab_id <- paste0("ftab-matrix-",ftab_mat_html_env$counter)
166 res <- html_table(res,class="ftable",id=ftab_id)
167
168 ftab_mat_html_env$counter <- ftab_mat_html_env$counter + 1
169
170 style_global <- style_ftab_global(id=ftab_id,style=style,margin=margin)
171 style_toprule <- style_ftab_rule(id=ftab_id,rulewidth=toprule,top=TRUE,
172 rows=1)
173 style_bottomrule <- style_ftab_rule(id=ftab_id,rulewidth=bottomrule,bottom=TRUE,
174 rows=ntot)
175 nrows <- sapply(x,nrow)
176 dim(nrows) <- dim(x)
177 nrows <- nrows[,1]
178
179 if(length(nrows) > 1){
180 if(varinfront){
181 midrule_rows <- cumsum(c(nh,nrows))
182 }
183 else {
184 nrows <- nrows[-1]
185 l.nr <- length(nrows)
186 if(grouprules > 1)
187 midrule_rows <- c(nh,nrows + (rep(1:l.nr,each=2) - c(1,0)) + nh)
188 else
189 midrule_rows <- c(nh,nrows + 1:l.nr - 1 + nh)
190 }
191 }
192 else midrule_rows <- nh
193
194 style_midrule <- style_ftab_rule(id=ftab_id,rulewidth=midrule,bottom=TRUE,
195 rows=midrule_rows)
196 style_grouprule <- style_ftab_matrix_group_rule(id=ftab_id,rulewidth=midrule)
197
198 style_content <- paste(
199 style_global,
200 style_toprule,
201 style_midrule,
202 style_bottomrule,
203 style_grouprule,
204 sep="\n"
205 )
206
207 if(split.dec){
208 dot_style <- "
209 padding-left: 0px;
210 padding-right: 0px;
211 text-align: center;
212 width: .2ex;
213 /*background-color: blue;*/
214 "
215 before_dot_style <- "
216 padding-left: 0.5ex;
217 padding-right: 0px;
218 text-align: right;
219 /*background-color: red;*/
220 "
221 behind_dot_style <- "
222 padding-left: 0px;
223 padding-right: 0.5ex;
224 text-align: left;
225 /*background-color: green;*/
226 "
227 dot_cols <- dot_cols + nl
228 style_dots <- style_df_cols(id=ftab_id,cols=dot_cols,style=dot_style)
229 style_before_dots <- style_df_cols(id=ftab_id,cols=dot_cols-1,style=before_dot_style)
230 style_behind_dots <- style_df_cols(id=ftab_id,cols=dot_cols+1,style=behind_dot_style)
231 style_content <- paste(style_content,
232 style_dots,
233 style_before_dots,
234 style_behind_dots,sep="\n")
235 }
236
237 header_style <- "
238 padding-left: 0.5ex;
239 padding-right: 0.5ex;
240 text-align: center;
241 /*background-color: gray;*/
242 "
243 style_header <- style_ftab_header(id=ftab_id,style=header_style)
244 style_content <- paste(style_content,
245 style_header,
246 "\n")
247
248 style_element <- html("style",style_content,linebreak=TRUE)
249 res <- html_group(style_element,res)
250 res <- as.character(res)
251 return(res)
252 }
253
254
255 htfm_mkHeader <- function(col.vars,m,max.l.cv,varontop,split.dec){
256
257 nms.cv <- names(col.vars)
258 n.col.vars <- length(col.vars)
259
260 header <- vector("list",length=n.col.vars)
261 mm <- 1
262 for(i in rev(1:n.col.vars)){
263
264 cv <- col.vars[[i]]
265 ncv <- length(cv)
266 if(split.dec)
267 colspan <- mm*3
268 else
269 colspan <- mm
270 mm <- mm*ncv
271 cv <- rep(cv,m%/%mm)
272
273 header.i <- html_td(cv,vectorize=TRUE,colspan=colspan,class="header")
274
275 if(n.col.vars > 1 || !varontop){
276
277 if(i == 1 && varontop || !length(nms.cv)){
278 htmp <- html_td("")
279 }
280 else {
281 if(!nzchar(nms.cv[i])){
282 htmp <- NULL
283 }
284 else
285 htmp <- html_td(paste0(nms.cv[i],":"),class="header")
286 }
287 if(length(htmp))
288 header.i <- c(htmp,header.i)
289 }
290 header[[i]] <- header.i
291 }
292
293 if(varontop){
294
295 if(split.dec)
296 colspan <- mm*3
297 else
298 colspan <- mm
299
300 if(n.col.vars > 1){
301 htmp1 <- html_td("")
302 htmp2 <- html_td(nms.cv[1],colspan=colspan,class="header")
303 htmp <- c(htmp1,htmp2)
304 }
305 else if(nzchar(nms.cv[1])){
306 htmp <- html_td(names(col.vars)[1],colspan=colspan,class="header")
307 }
308 else {
309 htmp <- html_td("",colspan=colspan)
310 }
311 header <- c(list(htmp),header)
312 }
313
314 tmp.header <- header
315
316 if(split.dec)
317 colspan <- m*3
318 else
319 colspan <- m
320 if(n.col.vars > 1 || !varontop)
321 colspan <- colspan + 1
322
323 l.header <- max.l.cv + varontop
324
325 header <- rep(list(html_td("",colspan=colspan)),l.header)
326
327 i <- seq(to=l.header,length=length(tmp.header))
328 header[i] <- tmp.header
329 header
330 }
331
332 htfm_mkLeader <- function(row.vars,n,max.l.rv,varinfront) {
333
334 align.left <- c("text-align"="left")
335 n.row.vars <- length(row.vars)
336 nms.row.vars <- names(row.vars)
337 leader <- matrix("",nrow=n,ncol=n.row.vars)
338
339 for(ii in 1:n.row.vars){
340
341 rv <- row.vars[[ii]]
342 rep.rv <- if(ii==1)1 else nn.rv
343 rv <- rep(rv,rep.rv)
344 nn.rv <- length(rv)
345 rv.n <- n/nn.rv
346 pos <- (1:nn.rv)*rv.n
347 pos <- pos - rv.n + 1
348 leader[pos,ii] <- rv
349 }
350 if(varinfront){
351 lleader <- character(n)
352 if(nzchar(nms.row.vars[1]))
353 lleader[1] <- nms.row.vars[1]
354 else if(n==1){
355 lleader[1] <- leader[1,1]
356 leader[1,1] <- ""
357 }
358
359 leader <- cbind(lleader,leader)
360 if(n.row.vars > 1){
361 hleader <- character(n.row.vars+1)
362 ii <- 2:n.row.vars
363 hleader[ii] <- nms.row.vars[ii]
364 leader <- rbind(hleader,leader)
365 }
366 }
367
368 tmp.leader <- leader
369 leader <- matrix("",nrow=nrow(leader),ncol=max.l.rv+as.integer(varinfront))
370 ii <- seq(to=ncol(leader),length=ncol(tmp.leader))
371 leader[,ii] <- tmp.leader
372
373 leader <- array(html_td(leader,vectorize=TRUE),dim=dim(leader))
374 leader
375 }
376
377 htfm_mkBody <- function(x,format,digits,split.dec){
378
379 n <- nrow(x)
380 m <- ncol(x)
381
382 d <- digits
383 digits <- integer(m)
384 digits[] <- d
385
386 fo <- format
387 format <- integer(m)
388 format[] <- fo
389
390 body <- array(list(),dim=c(n,m))
391
392 for(i in 1:m){
393 tmp <- formatC(x[,i],digits=digits[i],format=format[i])
394 if(getOption("html.use.ampersand",FALSE))
395 tmp <- gsub("-","&minus;",tmp,fixed=TRUE)
396 else
397 tmp <- gsub("-","\u2212",tmp,fixed=TRUE)
398 if(!split.dec)
399 body[,i] <- html_td(tmp,vectorize=TRUE)
400 else {
401 tmp <- spltDec(tmp)
402 tmp <- html_td_spltDec(tmp)
403 body[,i] <- tmp
404 }
405 }
406 body
407 }
408