-
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.
Merge pull request #20 from vincentarelbundock/tinytable
`tinytable` support
- Loading branch information
Showing
5 changed files
with
134 additions
and
4 deletions.
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
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,67 @@ | ||
toTinytable <- function(table, ...) { | ||
|
||
if (!inherits(table, "tabular")) | ||
stop("'table' must be a 'tabular' object.") | ||
|
||
if (!requireNamespace("tinytable")) | ||
stop("Please install the 'tinytable' package.") | ||
|
||
rowLabels <- attr(table, "rowLabels") | ||
rowLabels[is.na(rowLabels)] <- "" | ||
|
||
clabels <- attr(table, "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(table, latex = FALSE) | ||
|
||
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] | ||
} | ||
} | ||
} | ||
|
||
out <- tinytable::tt(chars, ...) | ||
|
||
# TODO: allow justification on a cell-by-cell basis. Currently we only columns. | ||
just <- cbind( | ||
attr(attr(table, "rowLabels"), "justification"), | ||
attr(table, "justification")) | ||
for (j in seq_len(ncol(just))) { | ||
align <- unique(just[, j]) | ||
if (length(align) == 1 && align %in% c("l", "r", "c")) { | ||
out <- tinytable::style_tt(out, j = j, align = align) | ||
} | ||
} | ||
|
||
# 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) | ||
} | ||
|
||
# entries in the first row of clabels are already colnames in out | ||
if (nrow(clabels) > 1) { | ||
for (i in (nrow(clabels) - 1):1) { | ||
s <- get_span(clabels[i,]) | ||
out <- tinytable::group_tt(out, j = s) | ||
} | ||
} | ||
|
||
return(out) | ||
} |
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,44 @@ | ||
\name{toTinytable} | ||
\alias{toTinytable} | ||
\title{ | ||
Convert \code{tabular} object to \code{tinytable} format. | ||
} | ||
\description{ | ||
Converts the output of the \code{\link{tabular}} and related | ||
functions to a format consistent with the output of the | ||
\code{\link[tinytable]{tt}} function, so that it can be | ||
customized using the \pkg{tinytable} package. | ||
} | ||
\usage{ | ||
toTinytable(table, ...) | ||
} | ||
\arguments{ | ||
\item{table}{ | ||
An object of class \code{tabular}. | ||
} | ||
\item{...}{ | ||
Additional arguments to pass to \code{\link[tinytable]{tt}}. | ||
} | ||
} | ||
\value{ | ||
An object of class \code{tinytable}, suitable for passing | ||
to functions in the \pkg{tinytable} package. These tables | ||
can be exported to several formats, including LaTeX, HTML, | ||
Markdown, Word, Typst, PDF, and PNG. | ||
} | ||
|
||
\seealso{ | ||
\code{\link[tinytable]{tinytable-package}} | ||
} | ||
\examples{ | ||
if (requireNamespace("tinytable")) { | ||
|
||
tab <- tabular( (Species + 1) ~ (n=1) + Format(digits=2)* | ||
(Sepal.Length + Sepal.Width)*(mean + sd), data=iris ) | ||
tab <- toTinytable(tab, theme = "striped") | ||
tab <- tinytable::style_tt(tab, i = 1:2, background = "teal", color = "white") | ||
tab | ||
|
||
} | ||
|
||
} |
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