diff --git a/DESCRIPTION b/DESCRIPTION index 0138f6a6..a4e9bdf8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,6 +9,7 @@ Enhances: knitr Suggests: altdoc, + markdown, pandoc, rmarkdown, rstudioapi, diff --git a/R/build_tt.R b/R/build_tt.R index 4a313892..5c22d243 100644 --- a/R/build_tt.R +++ b/R/build_tt.R @@ -78,3 +78,14 @@ build_tt <- function(x, output = NULL) { return(out) } + + +finalize_bootstrap <- function(x) { + if (meta(x)$output != "html") return(x) + out <- gsub( + "$tinytable_BOOTSTRAP_CLASS", + "table", + x, + fixed = TRUE) + return(out) +} diff --git a/R/format_tt.R b/R/format_tt.R index 00598537..0df983c9 100644 --- a/R/format_tt.R +++ b/R/format_tt.R @@ -11,11 +11,11 @@ #' @param num_mark_big Character to use as a thousands separator. #' @param num_mark_dec Decimal mark character. Default is the global option 'OutDec'. #' @param num_suffix Logical; if TRUE display short numbers with `digits` significant digits and K (thousands), M (millions), B (billions), or T (trillions) suffixes. -#' @param sprintf String passed to the `?sprintf` function to format numbers or interpolate strings with a user-defined pattern (similar to the `glue` package, but using Base R). -#' @param url Logical; if TRUE, treats the column as a URL. #' @param date A string passed to the `format()` function, such as "%Y-%m-%d". See the "Details" section in `?strptime` #' @param bool A function to format logical columns. Defaults to title case. -#' @param other A function to format columns of other types. Defaults to identity (no formatting). +#' @param other A function to format columns of other types. Defaults to `as.character()`. +#' @param markdown Logical; if TRUE, render markdown syntax in cells. Ex: `_italicized text_` is properly italicized in HTML and LaTeX. +#' @param sprintf String passed to the `?sprintf` function to format numbers or interpolate strings with a user-defined pattern (similar to the `glue` package, but using Base R). #' @inheritParams tt #' @inheritParams style_tt #' @@ -39,11 +39,11 @@ format_tt <- function(x, num_suffix = FALSE, num_mark_big = "", num_mark_dec = getOption("OutDec", default = "."), - sprintf = NULL, - url = FALSE, date = "%Y-%m-%d", bool = function(column) tools::toTitleCase(tolower(column)), - other = identity + other = as.character, + markdown = FALSE, + sprintf = NULL ) { out <- x @@ -61,6 +61,7 @@ format_tt <- function(x, url = url, date = date, bool = bool, + markdown = markdown, other = other) out <- meta(out, "lazy_format", c(meta(out)$lazy_format, list(cal))) } else { @@ -77,7 +78,8 @@ format_tt <- function(x, url = url, date = date, bool = bool, - other = other) + other = other, + markdown = markdown) } return(out) } @@ -94,7 +96,8 @@ format_tt_lazy <- function(x, url = FALSE, date = "%Y-%m-%d", bool = identity, - other = identity + markdown = FALSE, + other = as.character ) { if (isTRUE(check_atomic_vector(x))) { @@ -116,11 +119,11 @@ format_tt_lazy <- function(x, assert_flag(num_zero) assert_string(num_mark_big) assert_string(num_mark_dec) - assert_flag(url) assert_string(date) assert_function(bool) assert_function(identity) assert_string(sprintf, null.ok = TRUE) + assert_flag(markdown) # column index NULL or regex or integer vector @@ -192,6 +195,25 @@ format_tt_lazy <- function(x, } # loop over columns + # markdown at the very end + if (isTRUE(markdown)) { + assert_dependency("markdown") + for (col in j) { + if (meta(x)$output == "html") { + fun <- function(x) { + out <- trimws(markdown::mark_html(text = x, template = FALSE)) + out <- sub("
", "", out, fixed = TRUE) + out <- sub("
", "", out, fixed = TRUE) + return(out) + } + x[, col] <- sapply(x[, col], fun) + } else if (meta(x)$output == "latex") { + fun <- function(x) trimws(markdown::mark_latex(text = x, template = FALSE)) + x[, col] <- sapply(x[, col], fun) + } + } + } + if (isTRUE(atomic_vector)) { return(x[[1]]) } else { diff --git a/man/format_tt.Rd b/man/format_tt.Rd index bd327609..59dd94ea 100644 --- a/man/format_tt.Rd +++ b/man/format_tt.Rd @@ -13,11 +13,11 @@ format_tt( num_suffix = FALSE, num_mark_big = "", num_mark_dec = getOption("OutDec", default = "."), - sprintf = NULL, - url = FALSE, date = "\%Y-\%m-\%d", bool = function(column) tools::toTitleCase(tolower(column)), - other = identity + other = as.character, + markdown = FALSE, + sprintf = NULL ) } \arguments{ @@ -37,15 +37,15 @@ format_tt( \item{num_mark_dec}{Decimal mark character. Default is the global option 'OutDec'.} -\item{sprintf}{String passed to the \code{?sprintf} function to format numbers or interpolate strings with a user-defined pattern (similar to the \code{glue} package, but using Base R).} - -\item{url}{Logical; if TRUE, treats the column as a URL.} - \item{date}{A string passed to the \code{format()} function, such as "\%Y-\%m-\%d". See the "Details" section in \code{?strptime}} \item{bool}{A function to format logical columns. Defaults to title case.} -\item{other}{A function to format columns of other types. Defaults to identity (no formatting).} +\item{other}{A function to format columns of other types. Defaults to \code{as.character()}.} + +\item{markdown}{Logical; if TRUE, render markdown syntax in cells. Ex: \verb{_italicized text_} is properly italicized in HTML and LaTeX.} + +\item{sprintf}{String passed to the \code{?sprintf} function to format numbers or interpolate strings with a user-defined pattern (similar to the \code{glue} package, but using Base R).} } \value{ A data frame with formatted columns. diff --git a/vignettes/tutorial.qmd b/vignettes/tutorial.qmd index 016202e4..188f2f60 100644 --- a/vignettes/tutorial.qmd +++ b/vignettes/tutorial.qmd @@ -300,6 +300,42 @@ tt(dat) |> ``` ::: +## Markdown + +Markdown can be rendered in cells by using the `markdown` argument of the `format_tt()` function (note: this requires installing the `markdown` as an optional dependency). + +```{r} +dat <- data.frame( markdown = c( + "This is _italic_ text.", + "This sentence ends with a superscript^2^.") +) + +tt(dat) |> + format_tt(j = 1, markdown = TRUE) |> + style_tt(j = 1, align = "c") +``` + +Markdown syntax can be particularly useful when formatting URL links in a table: + +```{r} +dat <- data.frame( + Links = c( + "[`marginaleffects`](https://www.marginaleffects.com/)", + "[`modelsummary`](https://www.modelsummary.com/)", + "[`tinytable`](https://vincentarelbundock.github.io/tinytable/)", + "[`countrycode`](https://vincentarelbundock.github.io/countrycode/)", + "[`WDI`](https://vincentarelbundock.github.io/WDI/)", + "[`altdoc`](https://etiennebacher.github.io/altdoc/)", + "[`plot2`](https://grantmcdermott.com/plot2/)", + "[`parameters`](https://easystats.github.io/parameters/)", + "[`insight`](https://easystats.github.io/insight/)" + ) +) + +tt(dat) |> format_tt(j = 1, markdown = TRUE) +``` + + # Style The main styling function for the `tinytable` package is `style_tt()`. Via this function, you can access three main interfaces to customize tables: