| 1 |
|
| 2 |
trimws_drpz <- function(x){ |
| 3 |
x <- trimws(x) |
| 4 |
x[nzchar(x)] |
| 5 |
} |
| 6 |
|
| 7 |
format_cb_table_ht <- function(tab){ |
| 8 |
cn <- colnames(tab) |
| 9 |
if(ncol(tab)>2){ |
| 10 |
tab <- cbind( |
| 11 |
formatC(tab[,1,drop=FALSE],format="d"), |
| 12 |
formatC(tab[,2],format="f",digits=1), |
| 13 |
formatC(tab[,3],format="f",digits=1) |
| 14 |
) |
| 15 |
} |
| 16 |
else { |
| 17 |
tab <- cbind( |
| 18 |
formatC(tab[,1,drop=FALSE],format="d"), |
| 19 |
formatC(tab[,2],format="f",digits=1) |
| 20 |
) |
| 21 |
} |
| 22 |
tab[tab=="NA"] <- "" |
| 23 |
tab <- rbind(" "=cn,"",tab) |
| 24 |
tab |
| 25 |
} |
| 26 |
|
| 27 |
format_html.codebookEntry <- function(x,name="", |
| 28 |
var_tag="code", |
| 29 |
varid_prefix="", |
| 30 |
title_tag="p", |
| 31 |
... |
| 32 |
){ |
| 33 |
|
| 34 |
annot <- x@annotation |
| 35 |
description <- annot["description"] |
| 36 |
wording <- annot["wording"] |
| 37 |
if(length(annot)) annot <- annot[names(annot) %nin% c("description","wording")] |
| 38 |
|
| 39 |
title_html <- html(var_tag,.content=name[1],class="cbe-name") |
| 40 |
if(length(description) && !is.na(description)) |
| 41 |
title_html <- html_group(title_html," — ",html("span",.content=sQuote(description),class="cbe-description")) |
| 42 |
|
| 43 |
title_html <- html(title_tag,.content=title_html,class="cbe-title",id=paste0(varid_prefix,name[1])) |
| 44 |
title_html <- html_group(html_p(""),title_html) |
| 45 |
|
| 46 |
wording_html <- if(length(wording) && !is.na(wording)) |
| 47 |
html_p(dQuote(wording), class="cbe-wording") |
| 48 |
else NULL |
| 49 |
x.spec <- gsub("--","–",x@spec) |
| 50 |
spec_html <- cbind(html_td(names(x.spec),vectorize=TRUE), |
| 51 |
html_td(x.spec,vectorize=TRUE)) |
| 52 |
spec_html <- apply(spec_html,1,html_tr) |
| 53 |
spec_html <- as.html_group(spec_html) |
| 54 |
|
| 55 |
spec_html <- html_table(spec_html,class="cbe-spec") |
| 56 |
spec_html <- html_group(html_p(""),spec_html) |
| 57 |
|
| 58 |
tab <- x@stats$tab |
| 59 |
descr <- x@stats$descr |
| 60 |
|
| 61 |
if(length(tab)){ |
| 62 |
counts_html <- formatC(tab[,1,1],format="d") |
| 63 |
counts_html <- html_td(counts_html,vectorize=TRUE) |
| 64 |
|
| 65 |
perc_html <- formatC(tab[,-1,1,drop=FALSE], |
| 66 |
format="f",digits=1) |
| 67 |
perc_html[perc_html=="NA"] <- "" |
| 68 |
perc_html <- spltDec(perc_html) |
| 69 |
perc_html <- html_td_spltDec(perc_html) |
| 70 |
|
| 71 |
tab_html <- c( |
| 72 |
counts_html, |
| 73 |
perc_html |
| 74 |
) |
| 75 |
|
| 76 |
if(dim(tab)[3] > 1){ |
| 77 |
counts_html <- formatC(tab[,1,1], |
| 78 |
format="f",digits=1) |
| 79 |
counts_html <- spltDec(counts_html) |
| 80 |
counts_html <- html_td_spltDec(counts_html) |
| 81 |
|
| 82 |
perc_html <- formatC(tab[,-1,2,drop=FALSE], |
| 83 |
format="f",digits=1) |
| 84 |
perc_html[perc_html=="NA"] <- "" |
| 85 |
perc_html <- spltDec(perc_html) |
| 86 |
perc_html <- html_td_spltDec(perc_html) |
| 87 |
wtab_html <- c( |
| 88 |
counts_html, |
| 89 |
perc_html |
| 90 |
) |
| 91 |
tab_html <- c(tab_html,wtab_html) |
| 92 |
} |
| 93 |
tab_html <- matrix(tab_html, nrow = nrow(tab)) |
| 94 |
|
| 95 |
tab_rown <- trimws(rownames(tab)) |
| 96 |
|
| 97 |
tab_lab <- strsplit(tab_rown," ") |
| 98 |
|
| 99 |
tab_lab <- lapply(tab_lab,trimws_drpz) |
| 100 |
|
| 101 |
tab_has_lab <- sapply(tab_lab,length) > 1 |
| 102 |
tab_has_m <- sapply(sapply(tab_lab,"[",2) == "M",isTRUE) |
| 103 |
|
| 104 |
tab_val <- ifelse(tab_has_lab,sapply(tab_lab,"[",1),"") |
| 105 |
tab_lab <- ifelse(tab_has_lab,lapply(tab_lab,"[",-1,drop=FALSE),tab_lab) |
| 106 |
|
| 107 |
tab_m <- ifelse(tab_has_m,"M","") |
| 108 |
tab_lab <- ifelse(tab_has_m,lapply(tab_lab,"[",-1,drop=FALSE),tab_lab) |
| 109 |
|
| 110 |
tab_lab <- sapply(tab_lab,paste,collapse=" ") |
| 111 |
|
| 112 |
tab_lab_html <- cbind(html_td(tab_val,vectorize=TRUE), |
| 113 |
html_td(tab_m,vectorize=TRUE), |
| 114 |
html_td(tab_lab,vectorize=TRUE)) |
| 115 |
|
| 116 |
tab_html <- cbind(tab_lab_html,tab_html) |
| 117 |
tab_html <- as.html_group(apply(tab_html,1,html_tr)) |
| 118 |
|
| 119 |
if(dim(tab)[3] == 1){ |
| 120 |
if(dim(tab)[2] == 3){ |
| 121 |
tabh_html <- html_group(html_td("Values and labels",colspan=3,class="header"), |
| 122 |
html_td("N",class="header"), |
| 123 |
html_td("Valid",colspan=3,class="header"), |
| 124 |
html_td("Total",colspan=3,class="header")) |
| 125 |
} |
| 126 |
else{ |
| 127 |
tabh_html <- html_group(html_td("Values and labels",colspan=3,class="header"), |
| 128 |
html_td("N",class="header"), |
| 129 |
html_td("Percent",colspan=3,class="header")) |
| 130 |
} |
| 131 |
tabh_html <- html_tr(tabh_html) |
| 132 |
} else { |
| 133 |
if(dim(tab)[2] == 3){ |
| 134 |
tabh_html2 <- html_group(html_td("Values and labels",colspan=3,class="header"), |
| 135 |
html_td("N",class="header"), |
| 136 |
html_td("Valid",colspan=3,class="header"), |
| 137 |
html_td("Total",colspan=3,class="header"), |
| 138 |
html_td("N",colspan=3,class="header"), |
| 139 |
html_td("Valid",colspan=3,class="header"), |
| 140 |
html_td("Total",colspan=3,class="header")) |
| 141 |
tabh_html1 <- html_group(html_td("",colspan=3), |
| 142 |
html_td("Unweighted",colspan=7,class="header"), |
| 143 |
html_td("Weighted",colspan=9,class="header")) |
| 144 |
} |
| 145 |
else{ |
| 146 |
tabh_html2 <- html_group(html_td("Values and labels",colspan=3,class="header"), |
| 147 |
html_td("N",class="header"), |
| 148 |
html_td("Percent",colspan=3,class="header"), |
| 149 |
html_td("N",colspan=3,class="header"), |
| 150 |
html_td("Percent",colspan=3,class="header")) |
| 151 |
tabh_html1 <- html_group(html_td("",colspan=3), |
| 152 |
html_td("Unweighted",colspan=4,class="header"), |
| 153 |
html_td("Weighted",colspan=6,class="header")) |
| 154 |
} |
| 155 |
|
| 156 |
tabh_html1 <- html_tr(tabh_html1) |
| 157 |
tabh_html2 <- html_tr(tabh_html2) |
| 158 |
tabh_html <- c(tabh_html1,tabh_html2) |
| 159 |
} |
| 160 |
|
| 161 |
tab_html <- html_table(html_group(tabh_html,tab_html),class="cbe-table cbe-vl-table") |
| 162 |
tab_html <- html_group(html_p(""),tab_html) |
| 163 |
} |
| 164 |
else tab_html <- NULL |
| 165 |
|
| 166 |
if(length(descr)){ |
| 167 |
descr_ldr_html <- trimws(paste(rownames(descr),": ",sep="")) |
| 168 |
descr_ldr_html <- html_td(descr_ldr_html,vectorize=TRUE) |
| 169 |
descr_html <- formatC(descr,format="f",digits=3) |
| 170 |
|
| 171 |
descr_html <- spltDec(descr_html) |
| 172 |
descr_html <- html_td_spltDec(descr_html) |
| 173 |
|
| 174 |
dim(descr_html) <- dim(descr) |
| 175 |
|
| 176 |
descr_html <- cbind(descr_ldr_html,descr_html) |
| 177 |
descr_html <- apply(descr_html,1,html_tr) |
| 178 |
|
| 179 |
if(ncol(descr)>1){ |
| 180 |
descrh_html <- html_group(html_td(""), |
| 181 |
html_td("Unweighted",colspan=3,class="header"), |
| 182 |
html_td(" Weighted",colspan=3,class="header")) |
| 183 |
descr_html <- c(list(html_tr(descrh_html)),descr_html) |
| 184 |
} |
| 185 |
|
| 186 |
descr_html <- html_table(descr_html,class="cbe-table cbe-d-table") |
| 187 |
descr_html <- html_group(html_p(""),descr_html) |
| 188 |
} else descr_html <- NULL |
| 189 |
|
| 190 |
if(length(annot)){ |
| 191 |
annot_html <- cbind(html_dt(paste0(names(annot),":"),vectorize=TRUE), |
| 192 |
html_dd(annot,vectorize=TRUE)) |
| 193 |
annot_html <- as.html_group(t(annot_html)) |
| 194 |
annot_html <- html("dl",.content=annot_html,class="cbe-annotations") |
| 195 |
annot_html <- html_group(html_p(""),annot_html) |
| 196 |
} else annot_html <- NULL |
| 197 |
|
| 198 |
header_html <- html_div(html_group(title_html,wording_html),class="cbe-header") |
| 199 |
body_html <- html_div(html_group(spec_html,tab_html,descr_html,annot_html,html_p("")),class="cbe-body") |
| 200 |
|
| 201 |
cbe_html <- html_div( |
| 202 |
html_group(header_html,body_html),class="codebook-entry") |
| 203 |
cbe_html <- as.character(cbe_html) |
| 204 |
return(cbe_html) |
| 205 |
} |
| 206 |
|
| 207 |
codebook_format_stdstyle <- c( |
| 208 |
"padding-left"=".3em", |
| 209 |
"padding-right"=".3em" |
| 210 |
) |
| 211 |
|
| 212 |
format_html.codebook <- function(x, |
| 213 |
toprule=2, |
| 214 |
midrule=1, |
| 215 |
indent="3ex", |
| 216 |
style=codebook_format_stdstyle, |
| 217 |
var_tag="code", |
| 218 |
varid_prefix="", |
| 219 |
title_tag="p", |
| 220 |
...) |
| 221 |
{ |
| 222 |
out <- mapply(format_html,[email protected],name=names(x), |
| 223 |
MoreArgs = list( |
| 224 |
var_tag=var_tag, |
| 225 |
varid_prefix=varid_prefix, |
| 226 |
title_tag=title_tag |
| 227 |
)) |
| 228 |
res <- html_div(unlist(out),class="codebook") |
| 229 |
|
| 230 |
cb_format_tmpl <- " |
| 231 |
.cbe-body table { |
| 232 |
margin: unset; |
| 233 |
} |
| 234 |
.cbe-body table, |
| 235 |
.cbe-body table tr, |
| 236 |
.cbe-body table td |
| 237 |
{ |
| 238 |
border-style: none; |
| 239 |
padding: unset; |
| 240 |
} |
| 241 |
.cbe-header { |
| 242 |
border-top: solid 2px; |
| 243 |
border-bottom: solid 1px; |
| 244 |
padding-left: <<indent>>; |
| 245 |
} |
| 246 |
.cbe-body { |
| 247 |
padding-left: <<indent>>; |
| 248 |
} |
| 249 |
.cbe-table { |
| 250 |
border-collapse: collapse; |
| 251 |
} |
| 252 |
.cbe-table td:nth-child(n of .header) { |
| 253 |
text-align: center; |
| 254 |
padding-left: <<padding-left>>; |
| 255 |
padding-right: <<padding-right>>; |
| 256 |
} |
| 257 |
.cbe-vl-table td:nth-child(1) { |
| 258 |
text-align: right; |
| 259 |
padding-left: <<padding-left>>; |
| 260 |
padding-right: <<padding-right>>; |
| 261 |
} |
| 262 |
.cbe-vl-table td:nth-child(2) { |
| 263 |
text-align: center; |
| 264 |
padding-left: <<padding-left>>; |
| 265 |
padding-right: <<padding-right>>; |
| 266 |
} |
| 267 |
.cbe-vl-table td:nth-child(3) { |
| 268 |
text-align: left; |
| 269 |
padding-left: <<padding-left>>; |
| 270 |
padding-right: <<padding-right>>; |
| 271 |
} |
| 272 |
.cbe-vl-table td:nth-child(4) { |
| 273 |
text-align: right; |
| 274 |
padding-left: <<padding-left>>; |
| 275 |
padding-right: <<padding-right>>; |
| 276 |
} |
| 277 |
.cbe-vl-table td:nth-child(5), |
| 278 |
.cbe-vl-table td:nth-child(8), |
| 279 |
.cbe-vl-table td:nth-child(11), |
| 280 |
.cbe-vl-table td:nth-child(14), |
| 281 |
.cbe-vl-table td:nth-child(17), |
| 282 |
.cbe-d-table td:nth-child(2), |
| 283 |
.cbe-d-table td:nth-child(5){ |
| 284 |
text-align: right; |
| 285 |
padding-left: <<padding-left>>; |
| 286 |
padding-right: 0px; |
| 287 |
} |
| 288 |
.cbe-vl-table td:nth-child(6), |
| 289 |
.cbe-vl-table td:nth-child(9), |
| 290 |
.cbe-vl-table td:nth-child(12), |
| 291 |
.cbe-vl-table td:nth-child(15), |
| 292 |
.cbe-vl-table td:nth-child(18), |
| 293 |
.cbe-d-table td:nth-child(3), |
| 294 |
.cbe-d-table td:nth-child(6) { |
| 295 |
text-align: center; |
| 296 |
padding-left: 0px; |
| 297 |
padding-right: 0px; |
| 298 |
width: 1px; |
| 299 |
} |
| 300 |
.cbe-vl-table td:nth-child(7), |
| 301 |
.cbe-vl-table td:nth-child(10), |
| 302 |
.cbe-vl-table td:nth-child(13), |
| 303 |
.cbe-vl-table td:nth-child(16), |
| 304 |
.cbe-vl-table td:nth-child(19), |
| 305 |
.cbe-d-table td:nth-child(4), |
| 306 |
.cbe-d-table td:nth-child(7) { |
| 307 |
text-align: left; |
| 308 |
padding-right: <<padding-left>>; |
| 309 |
padding-left: 0px; |
| 310 |
} |
| 311 |
.cbe-d-table td:nth-child(1){ |
| 312 |
text-align: left; |
| 313 |
padding-left: <<padding-left>>; |
| 314 |
padding-right: <<padding-right>>; |
| 315 |
} |
| 316 |
" |
| 317 |
toprule <- paste0(toprule,"px") |
| 318 |
midrule <- paste0(midrule,"px") |
| 319 |
style_content <- fillin(cb_format_tmpl, |
| 320 |
c(codebook_format_stdstyle, |
| 321 |
indent=indent,toprule=toprule,midrule=midrule)) |
| 322 |
style_element <- html("style",style_content,linebreak=TRUE) |
| 323 |
res <- html_group(style_element,res) |
| 324 |
res <- as.character(res) |
| 325 |
return(res) |
| 326 |
} |