| 1 |
format_md.codebook <- function(x, ...) { |
| 2 |
out <- mapply(format_md, x = [email protected], name = names(x), |
| 3 |
MoreArgs = list(...)) |
| 4 |
unlist(out) |
| 5 |
} |
| 6 |
|
| 7 |
format_md.codebookEntry <- function(x, name = "", add_rules = TRUE, ...) { |
| 8 |
blank_line <- "\n" |
| 9 |
width <- getOption("width") |
| 10 |
hrule <- if (add_rules) paste(rep("-",width),collapse="") |
| 11 |
else NULL |
| 12 |
|
| 13 |
|
| 14 |
annot <- x@annotation |
| 15 |
description <- annot["description"] |
| 16 |
wording <- annot["wording"] |
| 17 |
if(length(annot)) annot <- annot[names(annot) %nin% c("description","wording")] |
| 18 |
|
| 19 |
title <- strwrap(if(length(description) && !is.na(description)) |
| 20 |
paste0(name[1]," --- ",sQuote(description)) |
| 21 |
else name[1] ,width=width,prefix=" ") |
| 22 |
|
| 23 |
wording <- if(length(wording) && !is.na(wording)) |
| 24 |
strwrap(dQuote(wording),width=width,prefix=" ") |
| 25 |
else NULL |
| 26 |
|
| 27 |
spec <- paste(" ",names(x@spec),x@spec," ") |
| 28 |
tab <- unclass(x@stats$tab) |
| 29 |
descr <- unclass(x@stats$descr) |
| 30 |
|
| 31 |
if(length(tab)){ |
| 32 |
|
| 33 |
tab.title <- attr(tab,"title") |
| 34 |
tab.d <- dim(tab) |
| 35 |
tab.dn <- dimnames(tab) |
| 36 |
tab_md <- apply(tab,3,format_cb_table_md) |
| 37 |
|
| 38 |
tab_rown <- trimws(rownames(tab)) |
| 39 |
|
| 40 |
tab_lab <- strsplit(tab_rown," ") |
| 41 |
|
| 42 |
tab_lab <- lapply(tab_lab,trimws_drpz) |
| 43 |
|
| 44 |
tab_has_lab <- sapply(tab_lab,length) > 1 |
| 45 |
tab_has_m <- sapply(sapply(tab_lab,"[",2) == "M",isTRUE) |
| 46 |
|
| 47 |
tab_val <- ifelse(tab_has_lab,sapply(tab_lab,"[",1),"") |
| 48 |
tab_lab <- ifelse(tab_has_lab,lapply(tab_lab,"[",-1,drop=FALSE),tab_lab) |
| 49 |
|
| 50 |
tab_m <- ifelse(tab_has_m,"M","") |
| 51 |
tab_lab <- ifelse(tab_has_m,lapply(tab_lab,"[",-1,drop=FALSE),tab_lab) |
| 52 |
|
| 53 |
tab_lab <- sapply(tab_lab,paste,collapse=" ") |
| 54 |
|
| 55 |
tab_val <- format_col_md(tab_val, |
| 56 |
align="r", |
| 57 |
colname="Values") |
| 58 |
tab_m <- format_col_md(tab_m, |
| 59 |
align="r", |
| 60 |
colname="") |
| 61 |
tab_lab <- format_col_md(tab_lab, |
| 62 |
align="l", |
| 63 |
colname="Labels") |
| 64 |
tab_ldr <- paste0(tab_val,"|",tab_m,"|",tab_lab) |
| 65 |
tab <- paste0(" |",tab_ldr,tab_md,"|") |
| 66 |
} |
| 67 |
if(!is.matrix(descr)) descr <- NULL |
| 68 |
if(length(descr)){ |
| 69 |
descr.rn <- format(paste(rownames(descr),":",sep=""),justify="left") |
| 70 |
if(is.numeric(descr[])) |
| 71 |
descr[] <- formatC(descr[],format="f",digits=3) |
| 72 |
descr[] <- gsub("NA","",descr[]) |
| 73 |
if(!length(ncol(descr))) browser() |
| 74 |
if(ncol(descr) > 1){ |
| 75 |
descr.rn <- c("","",descr.rn) |
| 76 |
descr <- rbind(colnames(descr),"",descr) |
| 77 |
} |
| 78 |
descr <- cbind(descr.rn,descr) |
| 79 |
descr <- apply(descr,2,format,justify="right") |
| 80 |
descr <- paste0(" ",apply(descr,1,paste,collapse=" ")," ") |
| 81 |
} |
| 82 |
if(length(tab) && length(descr)){ |
| 83 |
statstab <- format(c(tab,"",descr),justify="left") |
| 84 |
} |
| 85 |
else if(length(tab)){ |
| 86 |
statstab <- tab |
| 87 |
} |
| 88 |
else if(length(descr)){ |
| 89 |
statstab <- descr |
| 90 |
} |
| 91 |
else |
| 92 |
statstab <- NULL |
| 93 |
|
| 94 |
annot.out <- character() |
| 95 |
if(length(annot)){ |
| 96 |
for(i in seq_len(length(annot))){ |
| 97 |
annot.i <- annot[i] |
| 98 |
nm.i <- trimws(names(annot.i)) |
| 99 |
annot.i <- strwrap(annot.i,width=getOption("width")-8-4) |
| 100 |
annot.i <- c(paste(" ",annot.i),"") |
| 101 |
if(nzchar(nm.i)){ |
| 102 |
annot.i <- c( |
| 103 |
paste(" ",nm.i,":",sep=""), |
| 104 |
annot.i |
| 105 |
) |
| 106 |
} |
| 107 |
annot.out <- c(annot.out,annot.i) |
| 108 |
} |
| 109 |
} |
| 110 |
c( |
| 111 |
hrule, |
| 112 |
"", |
| 113 |
title, |
| 114 |
if(length(wording)) c( |
| 115 |
"", |
| 116 |
wording |
| 117 |
), |
| 118 |
"", |
| 119 |
hrule, |
| 120 |
"", |
| 121 |
spec, |
| 122 |
"", |
| 123 |
statstab, |
| 124 |
"", |
| 125 |
if(length(annot.out)) annot.out |
| 126 |
) |
| 127 |
|
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
format_cb_table_md <- function(tab){ |
| 132 |
cn <- colnames(tab) |
| 133 |
if(ncol(tab)>2){ |
| 134 |
if(all(trunc(tab[,1])==tab[,1])){ |
| 135 |
tab <- cbind( |
| 136 |
formatC(tab[,1,drop=FALSE],format="d"), |
| 137 |
formatC(tab[,2],format="f",digits=1), |
| 138 |
formatC(tab[,3],format="f",digits=1) |
| 139 |
) |
| 140 |
} else { |
| 141 |
tab <- cbind( |
| 142 |
formatC(tab[,1,drop=FALSE],format="f",digits=1), |
| 143 |
formatC(tab[,2],format="f",digits=1), |
| 144 |
formatC(tab[,3],format="f",digits=1) |
| 145 |
) |
| 146 |
} |
| 147 |
} |
| 148 |
else { |
| 149 |
if(all(trunc(tab[,1])==tab[,1])){ |
| 150 |
tab <- cbind( |
| 151 |
formatC(tab[,1,drop=FALSE],format="d"), |
| 152 |
formatC(tab[,2],format="f",digits=1) |
| 153 |
) |
| 154 |
} else { |
| 155 |
tab <- cbind( |
| 156 |
formatC(tab[,1,drop=FALSE],format="f",digits=1), |
| 157 |
formatC(tab[,2],format="f",digits=1) |
| 158 |
) |
| 159 |
} |
| 160 |
|
| 161 |
} |
| 162 |
tab[tab=="NA"] <- "" |
| 163 |
tab <- rbind(" "=cn,"",tab) |
| 164 |
tab <- format(tab,justify="right") |
| 165 |
tab[] <- paste0(" ",tab," ") |
| 166 |
specline <- sapply(nchar(tab[2,]),md_tabspec,align="r") |
| 167 |
tab[2,] <- specline |
| 168 |
tab <- apply(tab,1,paste,collapse="|") |
| 169 |
tab <- paste0("|",tab) |
| 170 |
tab |
| 171 |
} |
| 172 |
|
| 173 |
# This assumes that the contents are already formatted! |
| 174 |
format_col_md <- function(x,align,colname){ |
| 175 |
if(align=="r") |
| 176 |
x <- format(x,justify="right") |
| 177 |
else if(align=="l") |
| 178 |
x <- format(x,justify="left") |
| 179 |
x <- c(colname,x) |
| 180 |
x <- paste0(" ",format(x)," ") |
| 181 |
n <- nchar(x[1]) |
| 182 |
spec <- md_tabspec(n,align=align) |
| 183 |
c(x[1],spec,x[-1]) |
| 184 |
} |
| 185 |
|
| 186 |
md_tabspec <- function(n,align="l"){ |
| 187 |
spec <- rep("-",n) |
| 188 |
if(align=="l") |
| 189 |
spec[1] <- ":" |
| 190 |
else if(align=="r") |
| 191 |
spec[n] <- ":" |
| 192 |
paste(spec,collapse="") |
| 193 |
} |