library("tinytable")
library(tinytable)
<- mtcars[1:4, 1:5]
x
<- file.path(tempdir(), "test.html")
fn tt(x) |> save_tt(fn, overwrite = TRUE)
library(tinytable)
<- file.path(tempdir(), "table.tex")
filename tt(mtcars[1:4, 1:4]) |> save_tt(filename)
Save a Tiny Table to File
Description
This function saves an object of class tinytable to a specified file and format, with an option to overwrite existing files.
Usage
save_tt(
x,
output,
overwrite = get_option("tinytable_save_overwrite", default = FALSE)
)
Arguments
x
|
The tinytable object to be saved. |
output
|
String or file path.
|
overwrite
|
A logical value indicating whether to overwrite an existing file. |
Value
A string with the table when output
is a format, and the file path when output
is a valid path.
Dependencies
-
.pdf
output requires a full LaTeX installation on the local computer. -
.png
output requires thewebshot2
package. -
.html
self-contained files require thebase64enc
package.
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
Options can be set with options()
and change the default behavior of tinytable. For example:
options(tinytable_tt_digits = 4) tt(head(iris))
You can set options in a script or via .Rprofile
. Note: be cautious with .Rprofile
settings as they may affect reproducibility.
Default values for function arguments
tt()
-
tinytable_tt_digits
-
tinytable_tt_caption
-
tinytable_tt_notes
-
tinytable_tt_width
-
tinytable_tt_theme
-
tinytable_tt_rownames
format_tt()
-
tinytable_format_digits
-
tinytable_format_num_fmt
-
tinytable_format_num_zero
-
tinytable_format_num_suffix
-
tinytable_format_num_mark_big
-
tinytable_format_num_mark_dec
-
tinytable_format_date
-
tinytable_format_bool
-
tinytable_format_other
-
tinytable_format_replace
-
tinytable_format_escape
-
tinytable_format_markdown
-
tinytable_format_quarto
-
tinytable_format_fn
-
tinytable_format_sprintf
save_tt()
-
tinytable_save_overwrite
theme_tt()
Placement:
-
tinytable_theme_placement_float
-
tinytable_theme_placement_horizontal
Resize:
-
tinytable_theme_resize_width
-
tinytable_theme_resize_direction
Multipage:
-
tinytable_theme_multipage_rowhead
-
tinytable_theme_multipage_rowfoot
Tabular:
-
tinytable_theme_tabular_style
print.tinytable()
-
tinytable_print_output
Output-specific options
HTML
-
tinytable_html_mathjax
: Insert MathJax scripts (warning: may conflict if MathJax is loaded elsewhere) -
tinytable_html_portable
: Insert base64 encoded images directly in HTML forplot_tt()
-
tinytable_pdf_clean
: Delete temporary and log files -
tinytable_pdf_engine
: Choose between "xelatex", "pdflatex", "lualatex"
Quarto
The format_tt(quarto=TRUE)
argument enables Quarto data processing with some limitations:
-
The
LaTeX macro may not process references and markdown as expected
-
Quarto processing may conflict with
tinytable
styling/formatting
Options:
-
tinytable_quarto_disable_processing
: Disable Quarto cell processing -
tinytable_print_rstudio_notebook
: Display tables "inline" or in "viewer" for RStudio notebooks -
tinytable_quarto_figure
: Control Typst figure environment in Quarto
Example of Quarto-specific code in cells:
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)
For more details on Quarto table processing: https://quarto.org/docs/authoring/tables.html#disabling-quarto-table-processing