From 895775dd9e3c5079705307ff06a9913236aa1331 Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Sat, 20 Jan 2024 20:56:33 -0500 Subject: [PATCH] issue #93 bootstrap_class argument instead of themes --- R/build_tt.R | 4 +++- R/style_bootstrap.R | 9 +++++++++ R/style_tt.R | 12 +++++++++--- R/template.R | 44 ------------------------------------------ R/tt.R | 2 +- R/tt_bootstrap.R | 2 +- man/style_bootstrap.Rd | 3 +++ man/style_tt.Rd | 3 +++ man/tt.Rd | 2 +- vignettes/tutorial.qmd | 8 ++++---- 10 files changed, 34 insertions(+), 55 deletions(-) diff --git a/R/build_tt.R b/R/build_tt.R index 0b161489..4a313892 100644 --- a/R/build_tt.R +++ b/R/build_tt.R @@ -61,7 +61,9 @@ build_tt <- function(x, output = NULL) { out <- eval(l) } } - out <- meta(out, "output", output) + + # finalize + out <- finalize_bootstrap(out) # formal grid specification in pandoc includes lines everywhere hlines <- getOption("tinytable_grid_hlines", default = TRUE) diff --git a/R/style_bootstrap.R b/R/style_bootstrap.R index f4aa8da7..443b489d 100644 --- a/R/style_bootstrap.R +++ b/R/style_bootstrap.R @@ -16,6 +16,7 @@ style_bootstrap <- function(x, align = NULL, colspan = NULL, indent = 0, + bootstrap_class = "table", bootstrap_css = NULL, bootstrap_css_rule = NULL) { @@ -120,6 +121,14 @@ style_bootstrap <- function(x, out <- bootstrap_setting(out, bootstrap_css_rule, component = "css") } + if (!is.null(bootstrap_class)) { + out <- gsub( + "$tinytable_BOOTSTRAP_CLASS", + bootstrap_class, + out, + fixed = TRUE) + } + # Changing function names to table ID to avoid conflict with other tables functions out <- gsub("styleCell_\\w+\\(", paste0("styleCell_", meta(x, "id"), "("), out) diff --git a/R/style_tt.R b/R/style_tt.R index 74250152..49b3980e 100644 --- a/R/style_tt.R +++ b/R/style_tt.R @@ -29,6 +29,7 @@ #' @param align A single character or a string with a number of characters equal to the number of columns in `j`. Valid characters include 'c' (center), 'l' (left), or 'r' (right). #' @param colspan Number of columns a cell should span. Can only be used if both `i` and `j` are of length 1. Must be an integer greater than 1. #' @param indent Text indentation in em units. Positive values only. +#' @param bootstrap_class String. A Bootstrap table class such as `"table"`, `"table table-dark"` or `"table table-dark table-hover"`. See the bootstrap documentation. #' @param bootstrap_css A vector of CSS style declarations to be applied (ex: `"font-weight: bold"`). Each element corresponds to a cell defined by `i` and `j`. #' @param bootstrap_css_rule A string with complete CSS rules that apply to the table class specified using the `theme` argument of the `tt()` function. #' @param tabularray_inner A string that specifies the "inner" settings of a tabularray LaTeX table. @@ -62,6 +63,7 @@ style_tt <- function (x, indent = 0, tabularray_inner = NULL, tabularray_outer = NULL, + bootstrap_class = "table", bootstrap_css = NULL, bootstrap_css_rule = NULL, ...) { @@ -86,6 +88,7 @@ style_tt <- function (x, indent = indent, tabularray_inner = tabularray_inner, tabularray_outer = tabularray_outer, + bootstrap_class = bootstrap_class, bootstrap_css = bootstrap_css, bootstrap_css_rule = bootstrap_css_rule) @@ -117,6 +120,7 @@ style_tt_lazy <- function (x, indent, tabularray_inner, tabularray_outer, + bootstrap_class, bootstrap_css, bootstrap_css_rule) { @@ -166,7 +170,7 @@ style_tt_lazy <- function (x, x = out, i = i, j = j, bold = bold, italic = italic, monospace = monospace, underline = underline, strikeout = strikeout, color = color, background = background, fontsize = fontsize, width = width, align = align, colspan = colspan, indent = indent, tabularray_inner = tabularray_inner, tabularray_outer = tabularray_outer, bootstrap_css = bootstrap_css, - bootstrap_css_rule = bootstrap_css_rule) + bootstrap_css_rule = bootstrap_css_rule, bootstrap_class = bootstrap_class) out <- style_tabularray( x = out, i = i, j = j, bold = bold, italic = italic, monospace = monospace, underline = underline, strikeout = strikeout, @@ -176,7 +180,7 @@ style_tt_lazy <- function (x, out <- style_bootstrap( x = out, i = i, j = j, bold = bold, italic = italic, monospace = monospace, underline = underline, strikeout = strikeout, color = color, background = background, fontsize = fontsize, width = width, align = align, colspan = colspan, indent = indent, - bootstrap_css = bootstrap_css, bootstrap_css_rule = bootstrap_css_rule) + bootstrap_css = bootstrap_css, bootstrap_css_rule = bootstrap_css_rule, bootstrap_class = bootstrap_class) out <- style_grid( x = out, i = i, j = j, bold = bold, italic = italic, monospace = monospace, underline = underline, strikeout = strikeout, @@ -204,7 +208,8 @@ assert_style_tt <- function (x, indent, tabularray_inner, tabularray_outer, - bootstrap_css, + bootstrap_class = NULL, + bootstrap_css = NULL, bootstrap_css_rule = NULL) { m <- meta(x) @@ -220,6 +225,7 @@ assert_style_tt <- function (x, assert_logical(monospace) assert_logical(underline) assert_logical(strikeout) + assert_character(bootstrap_class, null.ok = TRUE) assert_character(bootstrap_css, null.ok = TRUE) assert_string(bootstrap_css_rule, null.ok = TRUE) diff --git a/R/template.R b/R/template.R index 97770b58..03d9a75a 100644 --- a/R/template.R +++ b/R/template.R @@ -11,47 +11,3 @@ template_tabularray <- function(theme = "default") { } return(out) } - - -template_bootstrap <- function(theme = "default") { - assert_string(theme) - out <- readLines(system.file("templates/bootstrap.html", package = "tinytable")) - - if (theme == "default") { - out <- gsub( - "$tinytable_BOOTSTRAP_CLASS", - "table", - out, - fixed = TRUE) - - } else if (theme == "void") { - out <- gsub( - "$tinytable_BOOTSTRAP_CLASS", - "table table-borderless", - out, - fixed = TRUE) - - } else if (theme == "grid") { - out <- gsub( - "$tinytable_BOOTSTRAP_CLASS", - "table table-bordered", - out, - fixed = TRUE) - - } else if (theme == "striped") { - out <- gsub( - "$tinytable_BOOTSTRAP_CLASS", - "table table-striped", - out, - fixed = TRUE) - - } else { - out <- gsub( - "$tinytable_BOOTSTRAP_CLASS", - theme, - out, - fixed = TRUE) - } - - return(out) -} diff --git a/R/tt.R b/R/tt.R index d340264e..0f89b991 100644 --- a/R/tt.R +++ b/R/tt.R @@ -15,7 +15,7 @@ #' @param width A numeric value between 0 and 1 indicating the proportion of the line width that the table should cover. #' @param theme The theme to apply to the table. #' * LaTeX: "default", "striped", "void", or "grid". -#' * HTML: "default", "striped", "void", "grid", or a (composite) Bootstrap class such as `"table table-dark"` or `"table table-dark table-hover"`. See +#' * HTML: "default", "striped", "void", "grid", or a (composite) #' @param notes A single string or a (named) list of strings to append at the bottom of the table. #' #' @param placement A string to control the position of tables in LaTeX. Will be inserted in square brackets like: `\\begin{table}[H]` diff --git a/R/tt_bootstrap.R b/R/tt_bootstrap.R index eeacad80..3f3e8b49 100644 --- a/R/tt_bootstrap.R +++ b/R/tt_bootstrap.R @@ -1,6 +1,6 @@ tt_bootstrap <- function(x, caption, theme, width, notes, ...) { - template <- template_bootstrap(theme) + template <- readLines(system.file("templates/bootstrap.html", package = "tinytable")) m <- meta(x) diff --git a/man/style_bootstrap.Rd b/man/style_bootstrap.Rd index c91087b3..c3d33073 100644 --- a/man/style_bootstrap.Rd +++ b/man/style_bootstrap.Rd @@ -20,6 +20,7 @@ style_bootstrap( align = NULL, colspan = NULL, indent = 0, + bootstrap_class = "table", bootstrap_css = NULL, bootstrap_css_rule = NULL ) @@ -69,6 +70,8 @@ style_bootstrap( \item{indent}{Text indentation in em units. Positive values only.} +\item{bootstrap_class}{String. A Bootstrap table class such as \code{"table"}, \code{"table table-dark"} or \code{"table table-dark table-hover"}. See the bootstrap documentation.} + \item{bootstrap_css}{A vector of CSS style declarations to be applied (ex: \code{"font-weight: bold"}). Each element corresponds to a cell defined by \code{i} and \code{j}.} \item{bootstrap_css_rule}{A string with complete CSS rules that apply to the table class specified using the \code{theme} argument of the \code{tt()} function.} diff --git a/man/style_tt.Rd b/man/style_tt.Rd index 73ffe47b..69e49dc1 100644 --- a/man/style_tt.Rd +++ b/man/style_tt.Rd @@ -22,6 +22,7 @@ style_tt( indent = 0, tabularray_inner = NULL, tabularray_outer = NULL, + bootstrap_class = "table", bootstrap_css = NULL, bootstrap_css_rule = NULL, ... @@ -76,6 +77,8 @@ style_tt( \item{tabularray_outer}{A string that specifies the "outer" settings of a tabularray LaTeX table.} +\item{bootstrap_class}{String. A Bootstrap table class such as \code{"table"}, \code{"table table-dark"} or \code{"table table-dark table-hover"}. See the bootstrap documentation.} + \item{bootstrap_css}{A vector of CSS style declarations to be applied (ex: \code{"font-weight: bold"}). Each element corresponds to a cell defined by \code{i} and \code{j}.} \item{bootstrap_css_rule}{A string with complete CSS rules that apply to the table class specified using the \code{theme} argument of the \code{tt()} function.} diff --git a/man/tt.Rd b/man/tt.Rd index 78c9c949..947eeac4 100644 --- a/man/tt.Rd +++ b/man/tt.Rd @@ -28,7 +28,7 @@ tt( \item{theme}{The theme to apply to the table. \itemize{ \item LaTeX: "default", "striped", "void", or "grid". -\item HTML: "default", "striped", "void", "grid", or a (composite) Bootstrap class such as \code{"table table-dark"} or \code{"table table-dark table-hover"}. See +\item HTML: "default", "striped", "void", "grid", or a (composite) }} \item{placement}{A string to control the position of tables in LaTeX. Will be inserted in square brackets like: \verb{\\\\begin\{table\}[H]}} diff --git a/vignettes/tutorial.qmd b/vignettes/tutorial.qmd index 2c437cb6..016202e4 100644 --- a/vignettes/tutorial.qmd +++ b/vignettes/tutorial.qmd @@ -598,20 +598,20 @@ The HTML customization options described in this section are not available for L ## Themes ::: {.content-visible when-format="html"} -The Bootstrap framework provides a number of built-in themes to style tables. To use them, we call `bootstrapOptions()` with the `class` argument, and we specify the Bootstrap class. A list of available Bootstrap classes can be found here: +The Bootstrap framework provides a number of built-in themes to style tables, using "classes." To use them, we call `style_tt()` with the `bootstrap_class` argument. A list of available Bootstrap classes can be found here: For example, to produce a "dark" table, we use the `table-dark` class: ```{r, eval = knitr::is_html_output()} -tt(x, theme = "table table-dark") +tt(x) |> style_tt(bootstrap_class = "table table-dark") ``` We can also combine several Bootstrap classes. Here, we get a "striped" table with the "warning" color and hover feature: ```{r, eval = knitr::is_html_output()} -tt(x, theme = "table table-striped table-warning table-hover") +tt(x) |> style_tt( + bootstrap_class = "table table-striped table-warning table-hover") ``` - ::: ## CSS declarations