Debian Codegraph @

R/format-html-dataframe.R

raw
1 df_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"="0px"
10 )
11
12
13 style_df_global <- function(id=NULL,class=NULL,style=df_format_stdstyle,margin="2ex auto") {
14
15 table_tmpl <- "
16 table<<class-or-id>> {
17 border-collapse: collapse; border-style: none; margin: <<margin>>;
18 }"
19 table_tr_tmpl <- "
20 table<<class-or-id>> tr {
21 border-style: none;
22 }"
23 table_td_tmpl <- "
24 table<<class-or-id>> td {
25 padding-top: <<padding-top>>;
26 padding-bottom: <<padding-bottom>>;
27 padding-left: <<padding-left>>;
28 padding-right: <<padding-right>>;
29 margin: 0px;
30 margin-top: <<margin-top>>;
31 margin-bottom: <<margin-bottom>>;
32 border-style: <<border-style>>;
33 border-width: <<border-width>>;
34 }"
35
36 class_or_id <- class_id_selector(id=id,class=class)
37
38 table_style <- fillin(table_tmpl,
39 c("class-or-id"=class_or_id,
40 margin=margin))
41 table_tr_style <- fillin(table_tr_tmpl,
42 c("class-or-id"=class_or_id))
43 table_td_style <- fillin(table_td_tmpl,
44 c("class-or-id"=class_or_id,
45 style))
46 paste(table_style,table_tr_style,table_td_style)
47 }
48
49 style_df_rule <- function(id=NULL,class=NULL,top=FALSE,bottom=FALSE,rulewidth=1,rows=NULL){
50 if(length(rows)){
51 class_or_id <- class_id_selector(id=id,class=class)
52 rulewidth <- paste0(rulewidth,"px")
53 selector_tmpl <- "table<<class-or-id>> tr:nth-child(<<row>>)"
54 selector <- character(0)
55 for(row in rows){
56 selector <- c(selector,
57 fillin(selector_tmpl,c("class-or-id"=class_or_id,row=row)))
58 }
59 selector <- paste(selector,collapse=",\n")
60 style_tmpl <- "<<selector>>{"
61 if(top){
62 style_tmpl <- paste(style_tmpl,
63 " border-top: <<rulewidth>> solid;",
64 sep="\n")
65 }
66 if(bottom){
67 style_tmpl <- paste(style_tmpl,
68 " border-bottom: <<rulewidth>> solid;",
69 sep="\n")
70 }
71 style_tmpl <- paste(style_tmpl,"}",sep="\n")
72
73 fillin(style_tmpl,c(selector=selector,rulewidth=rulewidth))
74 } else ""
75 }
76
77 style_df_cols <- function(id=NULL,class=NULL,cols=NULL,style=""){
78 if(length(cols)){
79 class_or_id <- class_id_selector(id=id,class=class)
80 selector_tmpl <- "table<<class-or-id>> td:nth-child(<<col>>)"
81 selector <- character(0)
82 for(col in cols){
83 selector <- c(selector,
84 fillin(selector_tmpl,c("class-or-id"=class_or_id,col=col)))
85 }
86 selector <- paste(selector,collapse=",\n")
87 paste0(selector,"{",style,"}")
88 }
89 }
90
91 style_df_header <- function(id=NULL,class=NULL,style=""){
92 class_or_id <- class_id_selector(id=id,class=class)
93 selector_tmpl <- "table<<class-or-id>> td:nth-child(n of .header)"
94 selector <- fillin(selector_tmpl,c("class-or-id"=class_or_id,col=col))
95 paste0("\n",selector,"{",style,"}")
96 }
97
98 df_html_env <- new.env()
99 df_html_env$counter <- 1
100
101 format_html.data.frame <- function(x,
102 toprule=2,midrule=1,bottomrule=2,
103 split.dec=TRUE,
104 row.names=TRUE,
105 digits=getOption("digits"),
106 format="f",
107 style=df_format_stdstyle,
108 margin="2ex auto",
109 ...){
110
111
112 colsep <- ""
113 rowsep <- "\n"
114
115 n <- nrow(x)
116 m <- ncol(x)
117 d <- digits
118 is.factor <- sapply(x,is.factor)
119 is.int <- sapply(x,is.integer) & !is.factor
120 is.num <- sapply(x,is.numeric) & !(is.int | is.factor)
121 m.num <- sum(is.num)
122 digits <- integer(m.num)
123 digits[] <- d
124 fdigits <- integer(m)
125 fdigits[is.num] <- digits
126 fo <- format
127 format <- character(m)
128 format[is.num] <- fo
129
130 dot_cols <- integer(0)
131 col_sum <- 0
132
133 colspan <- integer(0)
134 body <- matrix(nrow=nrow(x),ncol=0)
135 for(i in 1:m) {
136 tmp <- x[[i]]
137 dim.x.i <- dim(tmp)
138 ncol.tmp <- if(length(dim.x.i)) ncol(tmp) else 1
139 if(is.int[i]){
140 tmp <- formatC(tmp,format="d")
141 col <- html_td(tmp,vectorize=TRUE)
142 colspan <- c(colspan,ncol.tmp)
143 col_sum <- col_sum + ncol.tmp
144 }
145 else if(is.num[i]){
146 tmp <- formatC(tmp,digits=fdigits[i],format=format[i])
147 if(split.dec){
148 tmp <- spltDec(tmp)
149 col <- html_td_spltDec(tmp)
150 dot_cols_cur <- (1:ncol.tmp-1)*3 + 2
151 dot_cols <- c(dot_cols, col_sum + dot_cols_cur)
152 colspan <- c(colspan,3L*ncol.tmp)
153 col_sum <- col_sum + 3L*ncol.tmp
154 }
155 else{
156 col <- html_td(tmp,vectorize=TRUE)
157 colspan <- c(colspan,ncol.tmp)
158 col_sum <- col_sum + ncol.tmp
159 }
160 }
161 else {
162 tmp <- as.character(tmp)
163 col <- html_td(tmp,vectorize=TRUE)
164 colspan <- c(colspan,ncol.tmp)
165 col_sum <- col_sum + ncol.tmp
166 }
167 dim(col) <- dim.x.i
168 body <- cbind(body,col)
169 }
170 #browser()
171 if(row.names){
172 tmp <- rownames(x)
173 ldr <- html_td(tmp,vectorize=TRUE)
174 body <- cbind(ldr,body)
175 }
176
177 body <- apply(body,1,html_tr)
178
179 hdr <- colnames(x)
180 if(row.names) {
181 hdr <- c("",hdr)
182 colspan <- c(1L,colspan)
183 }
184
185 hdr <- html_td(hdr,vectorize=TRUE,class="header")
186 hdr[] <- mapply(setAttribs,hdr,colspan=colspan,SIMPLIFY=FALSE)
187 hdr <- html_tr(hdr)
188
189 res <- c(list(hdr),body)
190
191 df_id <- paste0("data-frame-",df_html_env$counter)
192 res <- html_table(res,id=df_id)
193
194 df_html_env$counter <- df_html_env$counter + 1
195
196 style_global <- style_df_global(id=df_id,style=style,margin=margin)
197 style_toprule <- style_df_rule(id=df_id,rulewidth=toprule,top=TRUE,
198 rows=1)
199 style_bottomrule <- style_df_rule(id=df_id,rulewidth=bottomrule,bottom=TRUE,
200 rows=nrow(x)+1)
201 style_midrule <- style_df_rule(id=df_id,rulewidth=midrule,bottom=TRUE,
202 rows=1)
203
204 style_content <- paste(
205 style_global,
206 style_toprule,
207 style_midrule,
208 style_bottomrule,
209 sep="\n"
210 )
211
212 if(split.dec){
213 dot_style <- "
214 padding-left: 0px;
215 padding-right: 0px;
216 text-align: center;
217 width: .2ex;
218 /*background-color: blue;*/
219 "
220 before_dot_style <- "
221 padding-left: 0.5ex;
222 padding-right: 0px;
223 text-align: right;
224 /*background-color: red;*/
225 "
226 behind_dot_style <- "
227 padding-left: 0px;
228 padding-right: 0.5ex;
229 text-align: left;
230 /*background-color: green;*/
231 "
232 style_dots <- style_df_cols(id=df_id,cols=dot_cols+1,style=dot_style)
233 style_before_dots <- style_df_cols(id=df_id,cols=dot_cols,style=before_dot_style)
234 style_behind_dots <- style_df_cols(id=df_id,cols=dot_cols+2,style=behind_dot_style)
235 style_content <- paste(style_content,
236 style_dots,
237 style_before_dots,
238 style_behind_dots,sep="\n")
239 }
240 header_style <- "
241 padding-left: 0.5ex;
242 padding-right: 0.5ex;
243 text-align: center;
244 /*background-color: gray;*/
245 "
246 style_header <- style_df_header(id=df_id,style=header_style)
247 leader_style <- "
248 padding-left: 0.5ex;
249 padding-right: 0px;
250 text-align: right;
251 /*background-color: red;*/
252 "
253 style_leader <- style_mat_cols(id=df_id,cols=1,style=leader_style)
254 style_content <- paste(style_content,
255 style_header,
256 style_leader,
257 "\n")
258
259 style_element <- html("style",style_content,linebreak=TRUE)
260 res <- html_group(style_element,res)
261 res <- as.character(res)
262 return(res)
263 }
264
265