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