-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ef7a0e
commit 9a162f4
Showing
2 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
toTinytable <- function(object, file = "", ...) { | ||
|
||
rowLabels <- attr(object, "rowLabels") | ||
rowLabels[is.na(rowLabels)] <- "" | ||
|
||
clabels <- attr(object, "colLabels") | ||
|
||
# pad column labels based on row stubs | ||
pad <- matrix("", nrow = nrow(clabels), ncol = ncol(rowLabels)) | ||
pad[nrow(pad),] <- colnames(rowLabels) | ||
clabels <- cbind(pad, clabels) | ||
|
||
chars <- format(object, latex = FALSE, minus = opts$latexminus, | ||
leftpad = opts$latexleftpad, | ||
rightpad = opts$latexrightpad,...) # format without justification | ||
|
||
chars <- data.frame(rowLabels, chars) | ||
colnames(chars) <- clabels[nrow(clabels),] | ||
|
||
# fill in missing column labels | ||
for (i in seq_len(nrow(clabels))) { | ||
for (j in seq_len(ncol(clabels))) { | ||
if (j != 1 && is.na(clabels[i, j])) { | ||
clabels[i, j] <- clabels[i, j - 1] | ||
} | ||
} | ||
} | ||
|
||
# convert to tinytable format | ||
out <- tinytable::tt(chars) | ||
|
||
# column spans | ||
get_span <- function(x) { | ||
x <- trimws(x) | ||
idx <- rle(x) | ||
end <- cumsum(idx$length) | ||
start <- end - idx$length + 1 | ||
span <- lapply(seq_along(idx$values), function(i) start[i]:end[i]) | ||
names(span) <- idx$values | ||
span <- span[names(span) != ""] | ||
return(span) | ||
} | ||
|
||
spans <- rev(apply(clabels, 1, get_span)[1:(nrow(clabels) - 1)]) | ||
|
||
for (s in spans) { | ||
out <- tinytable::group_tt(out, j = s) | ||
} | ||
|
||
return(out) | ||
} |