Debian Codegraph @

R/mtable-format-html.R

raw
1 mtable_format_stdstyle <- c(
2 "padding-top"="1px",
3 "padding-bottom"="1px",
4 "padding-left"="0.5ex",
5 "padding-right"="0.5ex",
6 "margin-top"="0px",
7 "margin-bottom"="0px",
8 "border-style"="none",
9 "border-width"="0px"
10 )
11
12 fillin <- function(template,substitutions){
13 res <- template
14 for(n in names(substitutions)){
15 pat <- paste0("<<",n,">>")
16 subst <- substitutions[n]
17 res <- gsub(pat,subst,res)
18 }
19 res
20 }
21
22 class_id_selector <- function(id=NULL,class=NULL){
23 class_or_id <- ""
24 if(length(class)){
25 class_or_id <- paste0(".",paste0(class,collapse="."))
26 }
27 if(length(id)){
28 id <- paste0("#",id[1])
29 class_or_id <- paste0(class_or_id,id)
30 }
31 class_or_id
32 }
33
34 style_mtable_global <- function(id=NULL,class=NULL,style=mtable_format_stdstyle,margin="2ex auto") {
35
36 table_tmpl <- "
37 table<<class-or-id>> {
38 border-collapse: collapse; border-style: none; margin: <<margin>>;
39 }"
40 table_tr_tmpl <- "
41 table<<class-or-id>> tr {
42 border-style: none;
43 }"
44 table_td_tmpl <- "
45 table<<class-or-id>> td {
46 padding-top: <<padding-top>>;
47 padding-bottom: <<padding-bottom>>;
48 padding-left: <<padding-left>>;
49 padding-right: <<padding-right>>;
50 margin: 0px;
51 margin-top: <<margin-top>>;
52 margin-bottom: <<margin-bottom>>;
53 border-style: <<border-style>>;
54 border-width: <<border-width>>;
55 }"
56
57 class_or_id <- class_id_selector(id=id,class=class)
58
59 table_style <- fillin(table_tmpl,
60 c("class-or-id"=class_or_id,
61 margin=margin))
62 table_tr_style <- fillin(table_tr_tmpl,
63 c("class-or-id"=class_or_id))
64 table_td_style <- fillin(table_td_tmpl,
65 c("class-or-id"=class_or_id,
66 style))
67 paste(table_style,table_tr_style,table_td_style)
68 }
69
70 style_mtable_rule <- function(id=NULL,class=NULL,top=FALSE,bottom=FALSE,rulewidth=1,rows=NULL){
71 if(length(rows)){
72 class_or_id <- class_id_selector(id=id,class=class)
73 rulewidth <- paste0(rulewidth,"px")
74 selector_tmpl <- "table<<class-or-id>> tr:nth-child(<<row>>)"
75 selector <- character(0)
76 for(row in rows){
77 selector <- c(selector,
78 fillin(selector_tmpl,c("class-or-id"=class_or_id,row=row)))
79 }
80 selector <- paste(selector,collapse=",\n")
81 style_tmpl <- "<<selector>>{"
82 if(top){
83 style_tmpl <- paste(style_tmpl,
84 " border-top: <<rulewidth>> solid;",
85 sep="\n")
86 }
87 if(bottom){
88 style_tmpl <- paste(style_tmpl,
89 " border-bottom: <<rulewidth>> solid;",
90 sep="\n")
91 }
92 style_tmpl <- paste(style_tmpl,"}",sep="\n")
93
94 fillin(style_tmpl,c(selector=selector,rulewidth=rulewidth))
95 } else ""
96 }
97
98 style_mtable_cmidrule <- function(id=NULL,class=NULL,top=FALSE,bottom=FALSE,rulewidth=0.5,cols=NULL,rows=NULL){
99 if(length(cols) && length(rows)){
100 class_or_id <- class_id_selector(id=id,class=class)
101 rulewidth <- paste0(rulewidth,"px")
102 selector_tmpl <- "table<<class-or-id>> tr:nth-child(<<row>>) td:nth-child(<<col>>)"
103 selector <- character(0)
104 for(row in rows){
105 for(col in cols){
106 selector <- c(selector,
107 fillin(selector_tmpl,c("class-or-id"=class_or_id,row=row,col=col)))
108 }
109 }
110 selector <- paste(selector,collapse=",\n")
111 style_tmpl <- "<<selector>>{"
112 if(top){
113 style_tmpl <- paste(style_tmpl,
114 " border-top: <<rulewidth>> solid;",
115 sep="\n")
116 }
117 if(bottom){
118 style_tmpl <- paste(style_tmpl,
119 " border-bottom: <<rulewidth>> solid;",
120 sep="\n")
121 }
122 style_tmpl <- paste(style_tmpl,"}",sep="\n")
123
124 fillin(style_tmpl,c(selector=selector,rulewidth=rulewidth))
125 } else ""
126 }
127
128
129
130 style_mtable_cols <- function(id=NULL,class=NULL,cols=NULL,style=""){
131 if(length(cols)){
132 class_or_id <- class_id_selector(id=id,class=class)
133 selector_tmpl <- "table<<class-or-id>> td:nth-child(<<col>>)"
134 selector <- character(0)
135 for(col in cols){
136 selector <- c(selector,
137 fillin(selector_tmpl,c("class-or-id"=class_or_id,col=col)))
138 }
139 selector <- paste(selector,collapse=",\n")
140 paste0(selector,"{",style,"}")
141 }
142 }
143
144 style_mtable_header <- function(id=NULL,class=NULL,style=""){
145 class_or_id <- class_id_selector(id=id,class=class)
146 selector_tmpl <- "table<<class-or-id>> td:nth-child(n of .header)"
147 selector <- fillin(selector_tmpl,c("class-or-id"=class_or_id,col=col))
148 paste0("\n",selector,"{",style,"}")
149 }
150
151
152
153 format_html.memisc_mtable <- function(x,
154 interaction.sep = NULL,
155 toprule=2,midrule=1,bottomrule=2,
156 split.dec=TRUE,
157 style=mtable_format_stdstyle,
158 margin="2ex auto",
159 sig.notes.style=c(width="inherit"),
160 ...){
161 x <- preformat_mtable(x)
162 res <- pf_mtable_format_html(x,interaction.sep=interaction.sep,
163 toprule=toprule,midrule=midrule,bottomrule=bottomrule,
164 split.dec=split.dec,style=style,margin=margin,
165 sig.notes.style=sig.notes.style,...)
166 as.character(res)
167 }
168
169 mtable_format_html <- function(x,
170 interaction.sep = NULL,
171 toprule=2,midrule=1,bottomrule=2,
172 split.dec=TRUE,
173 style=mtable_format_stdstyle,
174 margin="2ex auto",
175 sig.notes.style=c(width="inherit"),
176 ...)
177 pf_mtable_format_html(preformat_mtable(x),
178 interaction.sep=interaction.sep,
179 toprule=toprule,
180 midrule=midrule,
181 bottomrule=bottomrule,
182 split.dec=split.dec,
183 style=style,
184 margin=margin,
185 sig.notes.style=sig.notes.style,
186 ...)
187
188 mtable_html_env <- new.env()
189 mtable_html_env$counter <- 1
190
191 pf_mtable_format_html <- function(x,
192 interaction.sep = NULL,
193 toprule=2,midrule=1,bottomrule=2,
194 split.dec=TRUE,
195 style=mtable_format_stdstyle,
196 margin="2ex auto",
197 sig.notes.style=c(width="inherit"),
198 show.parmtypes = nrow(x$parmtab) > 1,
199 ...
200 ){
201
202 if(!length(interaction.sep)){
203 if(getOption("html.use.ampersand",FALSE))
204 interaction.sep <- " &times; "
205 else
206 interaction.sep <- " \u00d7 "
207 }
208
209 colsep <- ""
210 rowsep <- "\n"
211
212 pt <- x$parmtab
213 sst <- x$summary.stats
214 leaders <- x$leaders
215 headers <- x$headers
216 eq.headers <- x$eq.headers
217 outtypes <- x$outtypes
218 l.headers <- length(headers)
219 l.leaders <- length(leaders)
220
221 ncols <- sapply(pt[1,,drop=FALSE],ncol)
222
223 res <- NULL
224 has.eq.headers <- length(eq.headers) > 0
225
226 total_hdr_lines <- 1
227 total_pt_lines <- 0
228 total_sum_lines <- 0
229
230 dot_cols <- integer(0)
231 col_sum <- 0
232
233 for(j in 1:ncol(pt)){
234
235 name.j <- colnames(pt)[j]
236 pt.j <- pt[,j]
237
238 ncol.j <- unique(sapply(pt.j,ncol))
239 stopifnot(length(ncol.j)==1)
240
241 for(i in 1:length(pt.j)){
242
243 pt.ij <- pt.j[[i]]
244 ot.ij <- outtypes[i,j]
245 dm.ij <- dim(pt.ij)
246
247 if(ot.ij == "num"){
248 if(getOption("html.use.ampersand",FALSE))
249 pt.ij[] <- gsub("-","&minus;",pt.ij[],fixed=TRUE)
250 else
251 pt.ij[] <- gsub("-","\u2212",pt.ij[],fixed=TRUE)
252
253 if(split.dec){
254 pt.ij <- spltDec(pt.ij)
255 pt.ij <- gsub("([*]+)","<span class=\"signif.symbol\">\\1</span>",pt.ij)
256 pt.ij <- html_td_spltDec(pt.ij,add_CSS=FALSE)
257 }
258 else
259 pt.ij[] <- html_td(pt.ij,vectorize=TRUE)
260 } else {
261 if(split.dec)
262 pt.ij[] <- html_td(pt.ij,colspan=3,vectorize=TRUE)
263 else
264 pt.ij[] <- html_td(pt.ij,vectorize=TRUE)
265 }
266 dim(pt.ij) <- dm.ij
267 if(show.parmtypes){
268 spaces <- rep(" ",ncol(pt.ij))
269 spaces <- html_td(spaces,vectorize=TRUE)
270 pt.ij <- rbind(spaces,pt.ij)
271 }
272 pt.j[[i]] <- pt.ij
273 }
274 pt.j <- do.call(rbind,pt.j)
275 if(split.dec){
276 dot_cols_cur <- (1:ncol(pt.j)-1)*3 + 2
277 dot_cols <- c(dot_cols, col_sum + dot_cols_cur)
278 col_sum <- col_sum + ncol(pt.j)*3
279 }
280
281 if(has.eq.headers){
282 eq.header.j <- eq.headers[[name.j]]
283 n.eq.j <- length(eq.header.j)
284 eq.span <- ncol.j/n.eq.j
285 if(split.dec)
286 eq.span <- eq.span*3
287 eq.header.j <- html_td(eq.header.j,colspan=eq.span,vectorize=TRUE,class="header")
288 # pt.j <- rbind(eq.header.j,pt.j)
289 #total_hdr_lines <- max(total_hdr_lines,1+n.eq.j)
290 eq.headers[[name.j]] <- eq.header.j
291 }
292 total_pt_lines <- max(total_pt_lines,1+nrow(pt.j))
293
294 if(length(sst)){
295 sst.j <- sst[[j]]
296 if(getOption("html.use.ampersand",FALSE))
297 sst.j <- gsub("-","&minus;",sst.j,fixed=TRUE)
298 else
299 sst.j <- gsub("-","\u2212",sst.j,fixed=TRUE)
300 sst.j <- colexpand(sst.j,ncol.j)
301 dm.ij <- dim(sst.j)
302 if(split.dec){
303 sst.j <- spltDec(sst.j)
304 sst.j <- html_td_spltDec(sst.j,add_CSS=FALSE)
305 }
306 else
307 sst.j <- html_td(sst.j,vectorize=TRUE)
308 dim(sst.j) <- dm.ij
309 pt.j <- rbind(pt.j,sst.j)
310 total_sum_lines <- max(total_sum_lines,nrow(sst.j))
311 }
312
313 pt.j <- apply(pt.j,1,as.html_group)
314
315 res <- cbind(res,pt.j)
316 }
317
318 if(l.leaders){
319 leaders <- lapply(leaders,ldxp)
320 if(show.parmtypes){
321 parmtypes <- rownames(x$parmtab)
322 for(p in parmtypes){
323 leaders[[p]] <- rbind(p,leaders[[p]])
324 }
325 }
326 leaders <- do.call(rbind,leaders)
327
328 leaders <- html_td(leaders,vectorize=TRUE)
329
330 res <- cbind(leaders,res)
331 }
332
333 res <- apply(res,1,as.html_group)
334
335 if(l.headers){
336 for(k in 1:l.headers){
337 headers.k <- headers[[k]]
338 hspan.k <- sapply(headers.k,attr,"span")
339 if(split.dec)
340 hspan.k <- hspan.k*3
341 headers.k <- Map(html_td,headers.k,colspan=hspan.k,class="header")
342 if(l.leaders){
343 lheader.k <- html_td("",colspan=1,class="header")
344 headers.k <- c(list(lheader.k),headers.k)
345 }
346 headers[[k]] <- headers.k
347 }
348 headers <- lapply(headers,as.html_group)
349 if(has.eq.headers) {
350 if(l.leaders){
351 lheader.e <- html_td("",colspan=1,class="header")
352 tmp <- unlist(eq.headers,recursive=FALSE)
353 eq.headers <- as.html_group(c(list(lheader.e),tmp))
354 }
355 headers[[l.headers + 1]] <- eq.headers
356 }
357 res <- c(headers,res)
358 }
359 else if(has.eq.headers) {
360 if(l.leaders){
361 lheader.e <- html_td("",colspan=1,class="header")
362 tmp <- unlist(eq.headers,recursive=FALSE)
363 eq.headers <- as.html_group(c(list(lheader.e),tmp))
364 }
365 res <- c(list(eq.headers), res)
366 }
367
368 csum <- l.headers
369
370 sect.at <- integer()
371 for(i in 1:nrow(pt)){
372 sect.at <- c(sect.at,csum)
373 csum <- csum + nrow(pt[[i,1]])
374 if(nrow(pt) > 1)
375 csum <- csum + 1
376 }
377 if(length(sst) && any(sapply(sst,length)>0))
378 sect.at <- c(sect.at,csum)
379 if(has.eq.headers)
380 sect.at <- sect.at + 1
381 lines_total <- length(res)
382
383 signif.symbols <- x$signif.symbols
384 if(length(signif.symbols)){
385 signif.template <- getOption("signif.symbol.print.template",
386 signif.symbol.print.default.template)
387 signif.symbols <- format_signif_print(signif.symbols,
388 signif.template,
389 width=72)
390 signif.symbols <- gsub("<","&lt;",signif.symbols)
391 signif.symbols <- gsub(">","&gt;",signif.symbols)
392
393 if(split.dec)
394 totspan <- sum(ncols) * 3
395 else
396 totspan <- sum(ncols)
397 if(l.leaders)
398 totspan <- totspan + 1
399
400 signif.symbols <- html_p(signif.symbols)
401 signif.symbols <- html_td(signif.symbols,colspan=totspan)
402 res <- c(res,list(signif.symbols))
403 }
404
405 res <- html_tr(res,vectorize=TRUE)
406
407 mtable_id <- paste0("mtable-",mtable_html_env$counter)
408 res <- html_table(res,id=mtable_id)
409
410 mtable_html_env$counter <- mtable_html_env$counter + 1
411
412 #midrule_lines <- total_hdr_lines
413 #midrule_lines <- c(midrule_lines,total_hdr_lines + total_pt_lines)
414 midrule_lines <- sect.at
415
416 style_global <- style_mtable_global(id=mtable_id,style=style,margin=margin)
417 style_toprule <- style_mtable_rule(id=mtable_id,rulewidth=toprule,top=TRUE,
418 rows=1)
419 style_bottomrule <- style_mtable_rule(id=mtable_id,rulewidth=bottomrule,bottom=TRUE,
420 rows=lines_total)
421 style_midrule <- style_mtable_rule(id=mtable_id,rulewidth=midrule,bottom=TRUE,
422 rows=midrule_lines)
423
424
425 if(l.headers > 0 && has.eq.headers) {
426 if(l.leaders) cmod_fst <- 2 else cmod_fst <- 1
427 last.header <- headers[[l.headers]]
428 cmod_lst <- length((last.header))
429 cmid_cols <- cmod_fst:cmod_lst
430 style_mtable_cmidrule <- style_mtable_cmidrule(id=mtable_id,bottom=TRUE,
431 rows=l.headers,cols=cmid_cols)
432 }
433 else style_mtable_cmidrule <- NULL
434
435 style_content <- paste(
436 style_global,
437 style_toprule,
438 style_mtable_cmidrule,
439 style_midrule,
440 style_bottomrule,
441 sep="\n"
442 )
443 if(split.dec){
444 dot_style <- "
445 padding-left: 0px;
446 padding-right: 0px;
447 text-align: center;
448 /*background-color: blue;*/
449 "
450 before_dot_style <- "
451 padding-left: 0.5ex;
452 padding-right: 0px;
453 text-align: right;
454 /*background-color: red;*/
455 "
456 behind_dot_style <- "
457 padding-left: 0px;
458 padding-right: 0.5ex;
459 text-align: left;
460 /*background-color: green;*/
461 "
462
463 style_dots <- style_mtable_cols(id=mtable_id,cols=dot_cols+1,style=dot_style)
464 style_before_dots <- style_mtable_cols(id=mtable_id,cols=dot_cols,style=before_dot_style)
465 style_behind_dots <- style_mtable_cols(id=mtable_id,cols=dot_cols+2,style=behind_dot_style)
466 style_content <- paste(style_content,
467 style_dots,
468 style_before_dots,
469 style_behind_dots)
470 }
471 header_style <- "
472 padding-left: 0.5ex;
473 padding-right: 0.5ex;
474 text-align: center;
475 /*background-color: gray;*/
476 "
477 style_header <- style_mtable_header(id=mtable_id,style=header_style)
478 style_content <- paste(style_content,
479 style_header,
480 "\n")
481
482 style_element <- html("style",style_content,linebreak=FALSE)
483
484 res <- html_group(style_element,res)
485
486 return(res)
487
488 }
489
490 get_colspan <- function(x)x$attributes$colspan