Debian Codegraph @

R/format-html-ftable.R

raw
1 ftable_format_stdstyle <- c(
2 "padding-top"="3px",
3 "padding-bottom"="3px",
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"="0"
10 )
11
12 style_ftab_global <- function(id=NULL,class=NULL,style=ftable_format_stdstyle,margin="2ex auto") {
13
14 table_tmpl <- "
15 table<<class-or-id>> {
16 border-collapse: collapse; border-style: none; margin: <<margin>>;
17 }"
18 table_tr_tmpl <- "
19 table<<class-or-id>> tr {
20 border-style: none;
21 }"
22 table_td_tmpl <- "
23 table<<class-or-id>> td {
24 padding-top: <<padding-top>>;
25 padding-bottom: <<padding-bottom>>;
26 padding-left: <<padding-left>>;
27 padding-right: <<padding-right>>;
28 margin: 0px;
29 margin-top: <<margin-top>>;
30 margin-bottom: <<margin-bottom>>;
31 border-style: <<border-style>>;
32 border-width: <<border-width>>;
33 }"
34
35 class_or_id <- class_id_selector(id=id,class=class)
36
37 table_style <- fillin(table_tmpl,
38 c("class-or-id"=class_or_id,
39 margin=margin))
40 table_tr_style <- fillin(table_tr_tmpl,
41 c("class-or-id"=class_or_id))
42 table_td_style <- fillin(table_td_tmpl,
43 c("class-or-id"=class_or_id,
44 style))
45 paste(table_style,table_tr_style,table_td_style)
46 }
47
48 style_ftab_rule <- function(id=NULL,class=NULL,top=FALSE,bottom=FALSE,rulewidth=1,rows=NULL,firstcol=NULL){
49 if(length(rows)){
50 class_or_id <- class_id_selector(id=id,class=class)
51 rulewidth <- paste0(rulewidth,"px")
52 selector <- character(0)
53 if(length(firstcol)) {
54 selector_tmpl <- "table<<class-or-id>> tr:nth-child(<<row>>) td:nth-child(n+<<firstcol>>)"
55 for(row in rows){
56 selector <- c(selector,
57 fillin(selector_tmpl,c("class-or-id"=class_or_id,row=row,firstcol=firstcol)))
58 }
59 }
60 else {
61 selector_tmpl <- "table<<class-or-id>> tr:nth-child(<<row>>)"
62 for(row in rows){
63 selector <- c(selector,
64 fillin(selector_tmpl,c("class-or-id"=class_or_id,row=row)))
65 }
66 }
67 selector <- paste(selector,collapse=",\n")
68 style_tmpl <- "<<selector>>{"
69 if(top){
70 style_tmpl <- paste(style_tmpl,
71 " border-top: <<rulewidth>> solid;",
72 sep="\n")
73 }
74 if(bottom){
75 style_tmpl <- paste(style_tmpl,
76 " border-bottom: <<rulewidth>> solid;",
77 sep="\n")
78 }
79 style_tmpl <- paste(style_tmpl,"}",sep="\n")
80
81 fillin(style_tmpl,c(selector=selector,rulewidth=rulewidth))
82 } else ""
83 }
84
85 style_ftab_cols <- function(id=NULL,class=NULL,cols=NULL,style=""){
86 if(length(cols)){
87 class_or_id <- class_id_selector(id=id,class=class)
88 selector_tmpl <- "table<<class-or-id>> td:nth-child(<<col>>)"
89 selector <- character(0)
90 for(col in cols){
91 selector <- c(selector,
92 fillin(selector_tmpl,c("class-or-id"=class_or_id,col=col)))
93 }
94 selector <- paste(selector,collapse=",\n")
95 paste0(selector,"{",style,"}")
96 }
97 }
98
99 style_ftab_header <- function(id=NULL,class=NULL,style=""){
100 class_or_id <- class_id_selector(id=id,class=class)
101 selector_tmpl <- "table<<class-or-id>> td:nth-child(n of .header)"
102 selector <- fillin(selector_tmpl,c("class-or-id"=class_or_id,col=col))
103 paste0("\n",selector,"{",style,"}")
104 }
105
106 ftab_html_env <- new.env()
107 ftab_html_env$counter <- 1
108
109 format_html.ftable <- function(x,
110 show.titles=TRUE,
111 digits=0,
112 format="f",
113 toprule=2,midrule=1,bottomrule=2,
114 split.dec=TRUE,
115 style=ftable_format_stdstyle,
116 margin="2ex auto",
117 ...){
118
119 row.vars <- attr(x,"row.vars")
120 col.vars <- attr(x,"col.vars")
121 n.row.vars <- length(row.vars)
122 n.col.vars <- length(col.vars)
123 n <- nrow(x)
124 m <- ncol(x)
125 d <- digits
126 digits <- integer(m)
127 digits[] <- d
128
129 fo <- format
130 format <- integer(m)
131 format[] <- fo
132
133 body <- array("",dim=dim(x))
134 for(i in seq(along=digits)) {
135 #print(digits[i])
136 body[,i] <- formatC(x[,i],digits=digits[i],format=format[i])
137 }
138
139 body <- array(trimws(body),dim=dim(x))
140 if(getOption("html.use.ampersand",FALSE))
141 body[] <- gsub("-","&minus;",body[],fixed=TRUE)
142 else
143 body[] <- gsub("-","\u2212",body[],fixed=TRUE)
144
145 dot_cols <- integer(0)
146
147 if(split.dec){
148 tmp <- spltDec(body)
149 body <- html_td_spltDec(tmp)
150 dim(body) <- dim(x)
151 colspan <- 3L
152 dot_cols <- (1:m-1)*3 + 2
153 }
154 else {
155 body <- html_td(body,vectorize=TRUE)
156 dim(body) <- dim(x)
157 colspan <- 1L
158 }
159
160 leaders <- array(list(),dim=c(n,n.row.vars))
161 if(show.titles)
162 leaders <- cbind(leaders,"")
163
164 mm <- 1
165 for(j in rev(1:n.row.vars)){
166 rv <-row.vars[[j]]
167 nrv <- length(rv)
168 nn <- n/mm
169 i <- (1:nn)*mm - mm + 1
170 leaders[i,j] <- rv
171 mm <- mm*nrv
172 }
173 for(i in 1:n){
174 leaders[i,] <- html_td(leaders[i,],
175 vectorize=TRUE)
176 }
177 nl <- ncol(leaders)
178 body <- cbind(leaders,body)
179 nr <- nrow(body)
180
181 body <- as.html_group(apply(body,1,html_tr))
182
183 header <- list()
184 mm <- 1
185 for(i in rev(1:n.col.vars)){
186 cv <- col.vars[[i]]
187 ncv <- length(cv)
188 if(split.dec)
189 attribs <- list(colspan=mm*3)
190 else
191 attribs <- list(colspan=mm)
192 mm <- mm*ncv
193 cv <- rep(cv,m%/%mm)
194
195 if(show.titles){
196 if(n.col.vars == 1){
197 htmp1 <- html_td(c(names(row.vars),""),
198 vectorize=TRUE)
199 }
200 else {
201 if(i == n.col.vars){
202 htmp1 <- html_td(c(names(row.vars),paste0(names(col.vars)[i],":")),
203 vectorize=TRUE)
204 }
205 else
206 htmp1 <- html_td(c(rep("",n.row.vars),paste0(names(col.vars)[i],":")),
207 vectorize=TRUE)
208 }
209 }
210 else
211 htmp1 <- html_td(rep("",ncol(leaders)),
212 vectorize=TRUE)
213 attribs$class <- "header"
214 htmp2 <- setAttribs(html_td(cv,vectorize=TRUE),attribs)
215 header <- c(list(c(htmp1,htmp2)),header)
216 }
217 if(show.titles && n.col.vars == 1){
218 if(nzchar(names(col.vars))){
219 htmp1 <- html_td(rep("",ncol(leaders)),
220 vectorize=TRUE)
221 colspan <- ncol(x)
222 if(split.dec)
223 colspan <- colspan*3
224 attribs <- list(colspan=colspan)
225 attribs$class <- "header"
226 htmp2 <- setAttribs(html_td(names(col.vars),vectorize=TRUE),attribs)
227 header <- c(list(c(htmp1,htmp2)),header)
228 }
229 }
230 header <- html_tr(header,vectorize=TRUE)
231 nh <- length(header)
232
233 ftab_id <- paste0("ftable-",ftab_html_env$counter)
234 res <- html_table(c(header,body),class="ftable",id=ftab_id)
235
236 ftab_html_env$counter <- ftab_html_env$counter + 1
237
238 style_global <- style_ftab_global(id=ftab_id,style=style,margin=margin)
239 style_toprule <- style_ftab_rule(id=ftab_id,rulewidth=toprule,top=TRUE,
240 rows=1)
241 style_bottomrule <- style_ftab_rule(id=ftab_id,rulewidth=bottomrule,bottom=TRUE,
242 rows=nr+nh)
243 style_midrule <- style_ftab_rule(id=ftab_id,rulewidth=midrule,bottom=TRUE,
244 rows=nh)
245
246 style_content <- paste(
247 style_global,
248 style_toprule,
249 style_midrule,
250 style_bottomrule,
251 sep="\n"
252 )
253
254 if(nh > 1){
255 style_grouprule <- style_ftab_rule(id=ftab_id,rulewidth=midrule,bottom=TRUE,
256 firstcol=nl+1,
257 rows=1:(nh-1))
258 style_content <- paste(
259 style_content,
260 style_grouprule,
261 sep="\n"
262 )
263 }
264
265 if(split.dec){
266 dot_style <- "
267 padding-left: 0px;
268 padding-right: 0px;
269 text-align: center;
270 width: .2ex;
271 /*background-color: blue;*/
272 "
273 before_dot_style <- "
274 padding-left: 0.5ex;
275 padding-right: 0px;
276 text-align: right;
277 /*background-color: red;*/
278 "
279 behind_dot_style <- "
280 padding-left: 0px;
281 padding-right: 0.5ex;
282 text-align: left;
283 /*background-color: green;*/
284 "
285 dot_cols <- dot_cols + nl - 1
286 style_dots <- style_df_cols(id=ftab_id,cols=dot_cols+1,style=dot_style)
287 style_before_dots <- style_df_cols(id=ftab_id,cols=dot_cols,style=before_dot_style)
288 style_behind_dots <- style_df_cols(id=ftab_id,cols=dot_cols+2,style=behind_dot_style)
289 style_content <- paste(style_content,
290 style_dots,
291 style_before_dots,
292 style_behind_dots,sep="\n")
293 }
294 header_style <- "
295 padding-left: 0.5ex;
296 padding-right: 0.5ex;
297 text-align: center;
298 /*background-color: gray;*/
299 "
300 style_header <- style_ftab_header(id=ftab_id,style=header_style)
301 style_content <- paste(style_content,
302 style_header,
303 "\n")
304
305 style_element <- html("style",style_content,linebreak=TRUE)
306 res <- html_group(style_element,res)
307 res <- as.character(res)
308 return(res)
309 }
310