tt {tinytable} | R Documentation |
Draw a Tiny Table
Description
The tt
function renders a table in different formats with various styling options: HTML, Markdown, LaTeX, Word, PDF, PNG, or Typst. The table can be customized with additional functions:
-
style_tt()
: style fonts, colors, alignment, etc. -
format_tt()
: format numbers, dates, strings, etc. -
group_tt()
: row or column group labels. -
theme_tt()
: apply a collection of transformations to atinytable.
-
save_tt()
: save the table to a file or return the table as a string. -
print()
: print to a specific format, ex:print(x, "latex")
tinytable
attempts to determine the appropriate way to print the table based on interactive use, RStudio availability, and output format in RMarkdown or Quarto documents. Users can call print(x, output="markdown")
to print the table in a specific format. Alternatively, they can set a global option: options("tinytable_print_output"="markdown")
Usage
tt(
x,
digits = get_option("tinytable_tt_digits", default = NULL),
caption = get_option("tinytable_tt_caption", default = NULL),
notes = get_option("tinytable_tt_notes", default = NULL),
width = get_option("tinytable_tt_width", default = NULL),
theme = get_option("tinytable_tt_theme", default = "default"),
rownames = get_option("tinytable_tt_rownames", default = FALSE),
escape = get_option("tinytable_tt_escape", default = FALSE),
...
)
Arguments
x |
A data frame or data table to be rendered as a table. |
digits |
Number of significant digits to keep for numeric variables. When |
caption |
A string that will be used as the caption of the table. This argument should not be used in Quarto or Rmarkdown documents. In that context, please use the appropriate chunk options. |
notes |
Notes to append to the bottom of the table. This argument accepts several different inputs:
|
width |
Table or column width.
|
theme |
Function or string.
|
rownames |
Logical. If |
escape |
Logical. If |
... |
Additional arguments are ignored |
Value
An object of class tt
representing the table.
The table object has S4 slots which hold information about the structure of the table. Relying on or modifying the contents of these slots is strongly discouraged. Their names and contents could change at any time, and the tinytable
developers do not consider changes to the internal structure of the output object to be a "breaking change" for versioning or changelog purposes.
LaTeX preamble
tinytable
uses the tabularray
package from your LaTeX distribution to draw tables. tabularray
, in turn, uses the special tblr
, talltblr
, and longtblr
environments.
When rendering a document from Quarto or Rmarkdown directly to PDF, tinytable
will populate the LaTeX preamble automatically with all the required packages. For standalone LaTeX documents, these commands should be inserted in the preamble manually:
Note: Your document will fail to compile to PDF in Quarto if you enable caching and you use tinytable due to missing LaTeX headers. To avoid this problem, set the option #| cache: false
for the chunk(s) where you use tinytable.
\usepackage{tabularray} \usepackage{float} \usepackage{graphicx} \usepackage{rotating} \usepackage[normalem]{ulem} \UseTblrLibrary{booktabs} \UseTblrLibrary{siunitx} \newcommand{\tinytableTabularrayUnderline}[1]{\underline{#1}} \newcommand{\tinytableTabularrayStrikeout}[1]{\sout{#1}} \NewTableCommand{\tinytableDefineColor}[3]{\definecolor{#1}{#2}{#3}}
Global options
Many global options can be used to set the default argument values of tinytable
functions.
For a full list, see:
https://vincentarelbundock.github.io/tinytable/vignettes/options.html
Quarto
Figure environment
-
options("tinytable_quarto_figure" = FALSE)
: Typst only. Normally, it is best to allow Quarto to define the figure environment, so the default behavior is to not include one. -
options(tinytable_print_rstudio_notebook = "inline")
: Display tables "inline" or in the "viewer" in RStudio notebooks.
Data Processing
The format_tt(quarto=TRUE)
argument activates Quarto data processing for specific cells. This funcationality comes with a few warnings:
Currently, Quarto provides a
\QuartoMarkdownBase64{}
LaTeX macro, but it does not appear to do anything with it. References and markdown codes may not be processed as expected in LaTeX.Quarto data processing can enter in conflict with
tinytable
styling or formatting options. See below for how to disable it.
options(tinytable_quarto_disable_processing = TRUE)
Disable Quarto processing of cell content. Setting this global option to FALSE
may lead to conflicts with some tinytable
features, but it also allows use of markdown and Quarto-specific code in table cells, such as cross-references.
x <- data.frame(Math = "x^2^", Citation = "@Lovelace1842") fn <- function(z) sprintf("<span data-qmd='%s'></span>", z) tt(x) |> format_tt(i = 1, fn = fn)
See this link for more details: https://quarto.org/docs/authoring/tables.html#disabling-quarto-table-processing
HTML
-
options(tinytable_html_mathjax = TRUE)
insert MathJax scripts in the HTML document. Warning: This may conflict with other elements of the page if MathJax is otherwise loaded.
-
options(tinytable_html_portable = TRUE)
-
plot_tt()
inserts base 64 encoded images directly in the HTML file rather than use external links.
-
-
options(tinytable_pdf_clean = TRUE)
deletes temporary and log files.
-
options(tinytable_pdf_engine = "xelatex")
"xelatex", "pdflatex", "lualatex"
Examples
library(tinytable)
x <- mtcars[1:4, 1:5]
tt(x)
tt(x,
theme = "striped",
width = 0.5,
caption = "Data about cars.")
tt(x, notes = "Hello World!")
fn <- list(i = 0:1, j = 2, text = "Hello World!")
tab <- tt(x, notes = list("*" = fn))
print(tab, "latex")
k <- data.frame(x = c(0.000123456789, 12.4356789))
tt(k, digits=2)