From 0c5ef60c59fba5695c3a29d5c97a4e695b931ac5 Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Wed, 17 Jan 2024 22:27:53 -0500 Subject: [PATCH] documents --- R/format_tt.R | 10 ++++++++++ R/style_tt.R | 10 +++++++++- R/tt.R | 8 ++++++-- man/format_tt.Rd | 11 +++++++++++ man/style_tt.Rd | 9 +++++++++ man/tt.Rd | 9 ++++++++- vignettes/tutorial.qmd | 10 ++++++++-- 7 files changed, 61 insertions(+), 6 deletions(-) diff --git a/R/format_tt.R b/R/format_tt.R index 1329fd39..fc6a9cdd 100644 --- a/R/format_tt.R +++ b/R/format_tt.R @@ -25,6 +25,16 @@ #' #' @return A data frame with formatted columns. #' @export +#' @examples +#' dat <- data.frame( +#' a = rnorm(3, mean = 10000), +#' b = rnorm(3, 10000)) +#' tab <- format_tt(dat, +#' digits = 2, +#' num_mark_dec = ",", +#' num_mark_big = " ") +#' tt(tab) +#' format_tt <- function(x = NULL, j = NULL, output = NULL, diff --git a/R/style_tt.R b/R/style_tt.R index 7e754951..57bb9dcd 100644 --- a/R/style_tt.R +++ b/R/style_tt.R @@ -31,8 +31,16 @@ #' @param tabularray_inner A string that specifies the "inner" settings of a tabularray LaTeX table. #' @param tabularray_outer A string that specifies the "outer" settings of a tabularray LaTeX table. #' @return Returns a modified `tinytable` object with the applied styles. -#' @export #' @template latex_preamble +#' @export +#' @examples +#' library(tinytable) +#' x <- mtcars[1:5, 1:5] +#' tab <- tt(x) +#' tab <- style_tt(tab, j = 1:5, align = "lcccr") +#' tab <- style_tt(tab, i = 2:3, +#' background = "black", color = "orange", bold = TRUE) +#' style_tt <- function (x, i = NULL, j = NULL, diff --git a/R/tt.R b/R/tt.R index 6e563362..fab31f99 100644 --- a/R/tt.R +++ b/R/tt.R @@ -1,7 +1,11 @@ #' Draw a Tiny Table #' -#' The `tt` function renders a table in different formats (HTML, Markdown, or LaTeX) with various styling options. -#' +#' The `tt` function renders a table in different formats (HTML, Markdown, or LaTeX) with various styling options. The table can be customize with additional functions: +#' +#' * `style_tt()` to style fonts, colors, alignment, etc. +#' * `format_tt()` to format numbers, dates, strings, etc. +#' * `save_tt()` to save the table to a file. +#' #' @param x A data frame or data table to be rendered as a table. #' @param output The format of the output table. Can be "html", "latex", or "markdown". If NULL, the format is automatically detected in Quarto or Rmarkdown documents. #' @param digits Number of significant digits to keep for numeric variables. When `digits` is an integer, `tt()` calls `format_tt(x, digits = digits)` before proceeding to draw the table. Users who need more control can proceed in two steps: (1) format the data with `format_tt()` or other functions, and (2) pass the formatted data to `tt()` for drawing. See `?format_tt` for more details on formating options (ex: decimal, scientific notation, dates, boolean variables, etc.). diff --git a/man/format_tt.Rd b/man/format_tt.Rd index d9fe049f..8bbb1840 100644 --- a/man/format_tt.Rd +++ b/man/format_tt.Rd @@ -58,3 +58,14 @@ This function formats the columns of a data frame based on the column type (logi It allows various formatting options like significant digits, decimal points, and scientific notation. It also includes custom formatting for date and boolean values. } +\examples{ +dat <- data.frame( + a = rnorm(3, mean = 10000), + b = rnorm(3, 10000)) +tab <- format_tt(dat, + digits = 2, + num_mark_dec = ",", + num_mark_big = " ") +tt(tab) + +} diff --git a/man/style_tt.Rd b/man/style_tt.Rd index 2dfe5432..dbe94852 100644 --- a/man/style_tt.Rd +++ b/man/style_tt.Rd @@ -101,3 +101,12 @@ When rendering Quarto and Rmarkdown documents, \code{tinytable} will populate th }\if{html}{\out{}} } +\examples{ +library(tinytable) +x <- mtcars[1:5, 1:5] +tab <- tt(x) +tab <- style_tt(tab, j = 1:5, align = "lcccr") +tab <- style_tt(tab, i = 2:3, + background = "black", color = "orange", bold = TRUE) + +} diff --git a/man/tt.Rd b/man/tt.Rd index 8a5b2965..e8d133e1 100644 --- a/man/tt.Rd +++ b/man/tt.Rd @@ -40,7 +40,14 @@ tt( An object of class \code{tt} representing the table. } \description{ -The \code{tt} function renders a table in different formats (HTML, Markdown, or LaTeX) with various styling options. +The \code{tt} function renders a table in different formats (HTML, Markdown, or LaTeX) with various styling options. The table can be customize with additional functions: +} +\details{ +\itemize{ +\item \code{style_tt()} to style fonts, colors, alignment, etc. +\item \code{format_tt()} to format numbers, dates, strings, etc. +\item \code{save_tt()} to save the table to a file. +} } \section{LaTeX preamble}{ diff --git a/vignettes/tutorial.qmd b/vignettes/tutorial.qmd index 4be4a39d..a7c39c49 100644 --- a/vignettes/tutorial.qmd +++ b/vignettes/tutorial.qmd @@ -260,7 +260,10 @@ tt(x, notes = list(a = "Blah.", b = "Blah blah.")) In LaTeX and MathJax (for HTML), there are two main ways to enclose mathematical expressions, either between dollar signs or escaped parentheses: `$...$` or `\(...\)`. The first strategy [is discouraged by MathJax,](https://docs.mathjax.org/en/latest/basic/mathematics.html) because dollar signs are very common in non-mathematical contexts, which can cause rendering errors. In that spirit, `tinytable` will not render dollar-enclosed strings as mathematical expressions in HTML. Following the default MathJax settings, `tinytable` expects users to employ the escaped parentheses strategy: ```{r} -dat <- data.frame(Math = c("\\( x^2 + y^2 = z^2 \\)", "\\( \\frac{1}{2} \\)")) +dat <- data.frame(Math = c( + "\\( x^2 + y^2 = z^2 \\)", + "\\( \\frac{1}{2} \\)" +)) tt(dat) |> style_tt(align = "c") ``` @@ -448,7 +451,10 @@ tt(k) |> The `color`, `background`, and `fontsize` arguments are vectorized. This allows easy specification of different colors in a single call: ```{r} -tt(x) |> style_tt(i = 1:4, color = c("red", "blue", "green", "orange")) +tt(x) |> + style_tt( + i = 1:4, + color = c("red", "blue", "green", "orange")) ``` When using a single value for a vectorized argument, it gets applied to all values: