| 1 |
cbind.ftable <- function(..., deparse.level=1){ |
| 2 |
|
| 3 |
args <- list(...) |
| 4 |
argnames <- names(args) |
| 5 |
rv <- lapply(args,attr,"row.vars") |
| 6 |
rv.ok <- TRUE |
| 7 |
narg <- length(args) |
| 8 |
if(length(args)>1){ |
| 9 |
for(i in 1:narg){ |
| 10 |
if(!length(rv[[i]])){ |
| 11 |
if(i==1) stop("first argument must by an ftable") |
| 12 |
if(is.matrix(args[[i]])){ |
| 13 |
if(nrow(args[[i]])!=nrow(args[[1]])) stop("row vars must be identical") |
| 14 |
tmp <- args[[i]] |
| 15 |
} |
| 16 |
else{ |
| 17 |
tmp <- matrix(NA,nrow=nrow(args[[1]]),ncol=1) |
| 18 |
tmp[] <- as.numeric(args[[i]]) |
| 19 |
colnames(tmp) <- if(length(argnames)&&length(argnames[[i]]))argnames[[i]] else "" |
| 20 |
names(dimnames(tmp))[2] <- "" |
| 21 |
} |
| 22 |
attr(tmp,"row.vars") <- rv[[1]] |
| 23 |
attr(tmp,"col.vars") <- dimnames(tmp)[2] |
| 24 |
dimnames(tmp) <- NULL |
| 25 |
class(tmp) <- "ftable" |
| 26 |
args[[i]] <- tmp |
| 27 |
} |
| 28 |
else if(!identical(rv[[1]],rv[[i]])) { |
| 29 |
rv.ok <- FALSE |
| 30 |
break |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
if(!(rv.ok)) stop("row vars must be identical") |
| 36 |
|
| 37 |
row.vars <- list(attr(args[[1]],"row.vars")) |
| 38 |
col.vars <- lapply(args,attr,"col.vars") |
| 39 |
names(row.vars) <- NULL |
| 40 |
names(col.vars) <- NULL |
| 41 |
args <- lapply(args,function(arg)array(arg,dim=dim(arg))) |
| 42 |
res <- matrix(args,nrow=1,ncol=length(args)) |
| 43 |
structure(res, |
| 44 |
row.vars=row.vars, |
| 45 |
col.vars=col.vars, |
| 46 |
class="ftable_matrix") |
| 47 |
} |
| 48 |
|
| 49 |
rbind.ftable <- function(..., deparse.level=1){ |
| 50 |
|
| 51 |
args <- list(...) |
| 52 |
argnames <- names(args) |
| 53 |
cv <- lapply(args,attr,"col.vars") |
| 54 |
cv.ok <- TRUE |
| 55 |
narg <- length(args) |
| 56 |
if(length(args)>1){ |
| 57 |
for(i in 1:narg){ |
| 58 |
if(!length(cv[[i]])){ |
| 59 |
if(i==1) stop("first argument must by an ftable") |
| 60 |
if(is.matrix(args[[i]])){ |
| 61 |
if(ncol(args[[i]])!=ncol(args[[1]])) stop("col vars must be identical") |
| 62 |
tmp <- args[[i]] |
| 63 |
} |
| 64 |
else{ |
| 65 |
tmp <- matrix(NA,ncol=ncol(args[[1]]),nrow=1) |
| 66 |
tmp[] <- as.numeric(args[[i]]) |
| 67 |
rownames(tmp) <- if(length(argnames)&&length(argnames[[i]]))argnames[[i]] else "" |
| 68 |
names(dimnames(tmp))[1] <- "" |
| 69 |
# browser() |
| 70 |
} |
| 71 |
attr(tmp,"row.vars") <- dimnames(tmp)[1] |
| 72 |
attr(tmp,"col.vars") <- cv[[1]] |
| 73 |
dimnames(tmp) <- NULL |
| 74 |
class(tmp) <- "ftable" |
| 75 |
args[[i]] <- tmp |
| 76 |
} |
| 77 |
else if(!identical(cv[[1]],cv[[i]])) { |
| 78 |
cv.ok <- FALSE |
| 79 |
break |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if(!(cv.ok)) stop("column vars must be identical") |
| 85 |
|
| 86 |
col.vars <- list(attr(args[[1]],"col.vars")) |
| 87 |
row.vars <- lapply(args,attr,"row.vars") |
| 88 |
names(row.vars) <- NULL |
| 89 |
names(col.vars) <- NULL |
| 90 |
args <- lapply(args,function(arg)array(arg,dim=dim(arg))) |
| 91 |
res <- matrix(args,ncol=1,nrow=length(args)) |
| 92 |
structure(res, |
| 93 |
row.vars=row.vars, |
| 94 |
col.vars=col.vars, |
| 95 |
class="ftable_matrix") |
| 96 |
} |
| 97 |
|
| 98 |
cbind.ftable_matrix <- function(..., deparse.level=1){ |
| 99 |
|
| 100 |
args <- list(...) |
| 101 |
inh.fm <- sapply(args,inherits,"ftable_matrix") |
| 102 |
classes.ok <- all(inh.fm) |
| 103 |
if(!classes.ok) stop("all arguments must be 'ftable' or 'ftable_matrix' objects") |
| 104 |
|
| 105 |
rv <- lapply(args,attr,"row.vars") |
| 106 |
rv.ok <- TRUE |
| 107 |
narg <- length(args) |
| 108 |
if(length(args)>1){ |
| 109 |
for(i in 1:narg){ |
| 110 |
if(!identical(rv[[1]],rv[[i]])) { |
| 111 |
rv.ok <- FALSE |
| 112 |
break |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
if(!(rv.ok)) stop("row vars must be identical") |
| 118 |
|
| 119 |
row.vars <- rv[[1]] |
| 120 |
col.vars <- do.call(c,lapply(args,attr,"col.vars")) |
| 121 |
res <- lapply(args,function(x){ |
| 122 |
attributes(x)[c("col.vars","row.vars","class")] <- NULL |
| 123 |
x |
| 124 |
}) |
| 125 |
res <- do.call(cbind,res) |
| 126 |
structure(res, |
| 127 |
row.vars=row.vars, |
| 128 |
col.vars=col.vars, |
| 129 |
class="ftable_matrix") |
| 130 |
} |
| 131 |
|
| 132 |
rbind.ftable_matrix <- function(..., deparse.level=1){ |
| 133 |
|
| 134 |
args <- list(...) |
| 135 |
inh.fm <- sapply(args,inherits,"ftable_matrix") |
| 136 |
classes.ok <- all(inh.fm) |
| 137 |
if(!classes.ok) stop("all arguments must be 'ftable' or 'ftable_matrix' objects") |
| 138 |
|
| 139 |
cv <- lapply(args,attr,"col.vars") |
| 140 |
cv.ok <- TRUE |
| 141 |
narg <- length(args) |
| 142 |
if(length(args)>1){ |
| 143 |
for(i in 1:narg){ |
| 144 |
if(!identical(cv[[1]],cv[[i]])) { |
| 145 |
cv.ok <- FALSE |
| 146 |
break |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
if(!(cv.ok)) stop("column vars must be identical") |
| 152 |
|
| 153 |
row.vars <- do.call(c,lapply(args,attr,"row.vars")) |
| 154 |
col.vars <- cv[[1]] |
| 155 |
res <- lapply(args,function(x){ |
| 156 |
attributes(x)[c("col.vars","row.vars","class")] <- NULL |
| 157 |
x |
| 158 |
}) |
| 159 |
res <- do.call(rbind,res) |
| 160 |
structure(res, |
| 161 |
row.vars=row.vars, |
| 162 |
col.vars=col.vars, |
| 163 |
class="ftable_matrix") |
| 164 |
} |
| 165 |
|
| 166 |
fm_mkhdr <- function(cv,quote){ |
| 167 |
|
| 168 |
nms <- names(cv) |
| 169 |
l <- sapply(cv,length) |
| 170 |
prod.l <- cumprod(l) |
| 171 |
m <- length(cv) |
| 172 |
n <- prod.l[length(prod.l)] |
| 173 |
|
| 174 |
header <- matrix("",nrow=m,ncol=n) |
| 175 |
|
| 176 |
repn <- c(1,prod.l[-length(prod.l)]) |
| 177 |
for(i in 1:m){ |
| 178 |
|
| 179 |
ll <- prod.l[i] |
| 180 |
ii <- seq(from=1,length=ll,by=n%/%ll) |
| 181 |
header[i,ii] <- if(quote) paste0("\"",cv[[i]],"\"") else cv[[i]] |
| 182 |
} |
| 183 |
|
| 184 |
cbind(format(nms,justify="l"),header,"") |
| 185 |
} |
| 186 |
|
| 187 |
fm_mkldr <- function(rv,quote){ |
| 188 |
|
| 189 |
nms <- names(rv) |
| 190 |
l <- sapply(rv,length) |
| 191 |
prod.l <- cumprod(l) |
| 192 |
m <- length(rv) |
| 193 |
n <- prod.l[length(prod.l)] |
| 194 |
|
| 195 |
header <- matrix("",nrow=n,ncol=m) |
| 196 |
|
| 197 |
repn <- c(1,prod.l[-length(prod.l)]) |
| 198 |
for(i in 1:m){ |
| 199 |
|
| 200 |
ll <- prod.l[i] |
| 201 |
ii <- seq(from=1,length=ll,by=n%/%ll) |
| 202 |
header[ii,i] <- if(quote) paste0("\"",rv[[i]],"\"") else rv[[i]] |
| 203 |
} |
| 204 |
|
| 205 |
structure(rbind(nms,header,""),dimnames=NULL) |
| 206 |
} |
| 207 |
|
| 208 |
to.nrow <- function(x,nrow,from=c("top","bottom")){ |
| 209 |
|
| 210 |
from <- match.arg(from) |
| 211 |
res <- matrix(vector(mode=mode(x),length=length(x)),nrow=nrow,ncol=ncol(x)) |
| 212 |
|
| 213 |
if(from=="top") res[1:nrow(x),] <- x |
| 214 |
else res[1:nrow(x) + nrow-nrow(x),] <- x |
| 215 |
res |
| 216 |
} |
| 217 |
|
| 218 |
to.ncol <- function(x,ncol,from=c("left","right")){ |
| 219 |
|
| 220 |
from <- match.arg(from) |
| 221 |
res <- matrix(vector(mode=mode(x),length=length(x)),nrow=nrow(x),ncol=ncol) |
| 222 |
if(from=="left") res[,1:ncol(x)] <- x |
| 223 |
else res[,1:ncol(x) + ncol-ncol(x)] <- x |
| 224 |
res |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
format.ftable_matrix <- function(x,quote=TRUE,digits=0,format="f",...){ |
| 229 |
|
| 230 |
d <- digits |
| 231 |
digits <- integer(ncol(x)) |
| 232 |
digits[] <- d |
| 233 |
|
| 234 |
f <- format |
| 235 |
format <- character(ncol(x)) |
| 236 |
format[] <- f |
| 237 |
|
| 238 |
fmt <- array(list(),dim=dim(x)) |
| 239 |
for(i in 1:nrow(x)){ |
| 240 |
for(j in 1:ncol(x)){ |
| 241 |
|
| 242 |
tmp <- formatC(x[[i,j]],digits=digits[j],format=format[j]) |
| 243 |
tmp <- cbind("",tmp,"") |
| 244 |
tmp <- rbind("",tmp,"") |
| 245 |
fmt[[i,j]] <- tmp |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
col.vars <- attr(x,"col.vars") |
| 250 |
row.vars <- attr(x,"row.vars") |
| 251 |
|
| 252 |
header <- lapply(col.vars,fm_mkhdr,quote=quote) |
| 253 |
max.hh <- max(sapply(header,nrow)) |
| 254 |
|
| 255 |
header <- lapply(header,to.nrow,nrow=max.hh,from="bottom") |
| 256 |
header <- do.call(cbind,header) |
| 257 |
|
| 258 |
leader <- lapply(row.vars,fm_mkldr,quote=quote) |
| 259 |
max.lh <- max(sapply(leader,ncol)) |
| 260 |
leader <- lapply(leader,to.ncol,ncol=max.lh,from="right") |
| 261 |
leader <- do.call(rbind,leader) |
| 262 |
|
| 263 |
leader <- rbind(matrix("",nrow=nrow(header),ncol=ncol(leader)),leader) |
| 264 |
leader <- apply(leader,2,format.default,justify="l") |
| 265 |
|
| 266 |
body <- list() |
| 267 |
for(i in 1:nrow(fmt)) |
| 268 |
body[[i]] <- do.call(cbind,fmt[i,]) |
| 269 |
body <- do.call(rbind,body) |
| 270 |
|
| 271 |
#body <- apply(body,2,format.default,justify="r") |
| 272 |
res <- rbind(header,body) |
| 273 |
res <- apply(res,2,format.default,justify="r") |
| 274 |
res <- cbind(leader,res) |
| 275 |
res |
| 276 |
} |
| 277 |
|
| 278 |
Write.ftable_matrix <- function(x, |
| 279 |
file = "", |
| 280 |
quote = TRUE, |
| 281 |
append = FALSE, |
| 282 |
digits = 0, |
| 283 |
...){ |
| 284 |
r <- format.ftable_matrix(x,quote=quote,digits=digits) |
| 285 |
r <- apply(r,1,paste,collapse=" ") |
| 286 |
cat(r, file = file, append = append, sep = "\n") |
| 287 |
invisible(x) |
| 288 |
} |
| 289 |
|
| 290 |
print.ftable_matrix <- function(x,quote=FALSE,...) |
| 291 |
Write.ftable_matrix(x,file="",quote=quote,...) |
| 292 |
|