| style_tt | R Documentation |
Style a Tiny Table
Alias for style_tt()
style_tt(
x,
i = NULL,
j = NULL,
bold = NULL,
italic = NULL,
monospace = NULL,
smallcap = NULL,
underline = NULL,
strikeout = NULL,
color = NULL,
background = NULL,
fontsize = NULL,
align = NULL,
alignv = NULL,
colspan = NULL,
rowspan = NULL,
indent = NULL,
line = NULL,
line_color = "black",
line_width = 0.1,
line_trim = NULL,
finalize = NULL,
...
)
tt_style(
x,
i = NULL,
j = NULL,
bold = NULL,
italic = NULL,
monospace = NULL,
smallcap = NULL,
underline = NULL,
strikeout = NULL,
color = NULL,
background = NULL,
fontsize = NULL,
align = NULL,
alignv = NULL,
colspan = NULL,
rowspan = NULL,
indent = NULL,
line = NULL,
line_color = "black",
line_width = 0.1,
line_trim = NULL,
finalize = NULL,
...
)
x |
A table object created by |
i |
Numeric vector, logical matrix, string, or unquoted expression.
|
j |
Column indices where the styling should be applied. Can be:
|
bold |
Logical; if |
italic |
Logical; if |
monospace |
Logical; if |
smallcap |
Logical; if |
underline |
Logical; if |
strikeout |
Logical; if |
color |
Text color. Colors are standardized across output formats and can be specified as:
|
background |
Background color. Same color specification options as the |
fontsize |
Font size in em units. Can be |
align |
A single character or a string with a number of characters equal to the number of columns in |
alignv |
A single character specifying vertical alignment. Valid characters include 't' (top), 'm' (middle), 'b' (bottom). |
colspan |
Number of columns a cell should span. |
rowspan |
Number of rows a cell should span. |
indent |
Text indentation in em units. Positive values only. |
line |
String determines if solid lines (rules or borders) should be drawn around the cell, row, or column.
|
line_color |
Color of the line. See the |
line_width |
Width of the line in em units (default: 0.1). |
line_trim |
String specifying line trimming. Acceptable values: "l" (left), "r" (right), "lr" (both sides). When specified, shortens the lines by 0.8pt on the specified side(s). Default: NULL (no trimming). |
finalize |
A function applied to the table object at the very end of table-building, for post-processing. For example, the function could use regular expressions to add LaTeX commands to the text version of the table hosted in |
... |
extra arguments are ignored |
This function applies styling to a table created by tt(). It allows customization of text style (bold, italic, monospace), text and background colors, font size, cell width, text alignment, column span, and indentation. The function also supports passing native instructions to LaTeX (tabularray) and HTML (bootstrap) formats.
An object of class tt representing the table.
Markdown is a text-only format that only supports these styles: italic, bold, strikeout. The width argument is also unavailable.
These limitations exist because there is no standard markdown syntax for the other styling options.
However, in terminals (consoles) that support it, tinytable can display colors and text styles using
ANSI escape codes by setting theme_markdown(ansi = TRUE). This allows for rich formatting in
compatible terminal environments.
Word tables only support these styles: italic, bold, strikeout. The width argument is also unavailable.
Moreover, the style_tt() function cannot be used to style headers inserted by the group_tt() function;
instead, you should style the headers directly in the header definition using markdown syntax:
group_tt(i = list("*italic header*" = 2)). These limitations are due to the fact that we create
Word documents by converting a markdown table to .docx via the Pandoc software, which requires
going through a text-only intermediate format.
if (knitr::is_html_output()) options(tinytable_print_output = "html")
library(tinytable)
tt(mtcars[1:5, 1:6])
# Alignment
tt(mtcars[1:5, 1:6]) |>
style_tt(j = 1:5, align = "lcccr")
# Colors and styles
tt(mtcars[1:5, 1:6]) |>
style_tt(i = 2:3, background = "black", color = "orange", bold = TRUE)
# column selection with `j``
tt(mtcars[1:5, 1:6]) |>
style_tt(j = 5:6, background = "pink")
tt(mtcars[1:5, 1:6]) |>
style_tt(j = "drat|wt", background = "pink")
tt(mtcars[1:5, 1:6]) |>
style_tt(j = c("drat", "wt"), background = "pink")
tt(mtcars[1:5, 1:6], theme = "empty") |>
style_tt(
i = 2, j = 2,
colspan = 3,
rowspan = 2,
align = "c",
alignv = "m",
color = "white",
background = "black",
bold = TRUE)
tt(mtcars[1:5, 1:6], theme = "empty") |>
style_tt(
i = 0:3,
j = 1:3,
line = "tblr",
line_width = 0.4,
line_color = "teal")
tt(mtcars[1:5, 1:6], theme = "striped") |>
style_tt(
i = c(2, 5),
j = 3,
strikeout = TRUE,
fontsize = 0.7)
# Non-standard evaluation (NSE)
dat <- data.frame(
w = c(143002.2092, 201399.181, 100188.3883),
x = c(1.43402, 201.399, 0.134588),
y = as.Date(c(897, 232, 198), origin = "1970-01-01"),
z = c(TRUE, TRUE, FALSE)
)
tt(dat) |>
style_tt(i = w > 150000, j = c("w", "x"),
color = "white", background = "black")
tt(mtcars[1:5, 1:6]) |>
theme_html(class = "table table-dark table-hover")
inner <- "
column{1-4}={halign=c},
hlines = {fg=white},
vlines = {fg=white},
cell{1,6}{odd} = {bg=teal7},
cell{1,6}{even} = {bg=green7},
cell{2,4}{1,4} = {bg=red7},
cell{3,5}{1,4} = {bg=purple7},
cell{2}{2} = {r=4,c=2}{bg=azure7},
"
tt(mtcars[1:5, 1:4], theme = "empty") |>
theme_latex(inner = inner)
# Style group rows and non-group rows
dat <- data.frame(x = 1:6, y = letters[1:6])
dat |>
tt() |>
group_tt(i = list("Group A" = 3)) |>
style_tt(i = "groupi", background = "lightblue") |>
style_tt(i = "~groupi", background = "lightgray")
# unquote expressions
dat <- mtcars[1:10,]
dat <- dat[order(dat$am),]
tt(dat) |>
subset(mpg > 20) |>
group_tt(am)
# style elements: captions and colnames
notes <- list(
"*" = "Hello world",
"a" = "Bacon ipsum dolor amet kevin t-bone porchetta.")
tt(head(iris),
width = .5,
caption = "This is a Caption Example.",
notes = notes) |>
style_tt(2, 2, background = "pink", rowspan = 2, colspan = 2,
alignv = "m", align = "c", line = "tblr") |>
style_tt("colnames", italic = TRUE) |>
style_tt("caption", smallcap = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.