From c2ef2f634d59d4f09c1eceff1f620b75104c4199 Mon Sep 17 00:00:00 2001 From: Duncan Murdoch Date: Tue, 26 Nov 2024 16:10:19 -0500 Subject: [PATCH] Add `class` argument to `cell_spec()` --- DESCRIPTION | 4 ++-- R/cell_spec.R | 27 +++++++++++++++++++++------ inst/NEWS.md | 3 ++- man/cell_spec.Rd | 8 ++++++++ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 2d1897f..c96cbb9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: kableExtra Type: Package Title: Construct Complex Table with 'kable' and Pipe Syntax -Version: 1.4.0.7 +Version: 1.4.0.8 Authors@R: c( person('Hao', 'Zhu', email = 'haozhu233@gmail.com', role = c('aut', 'cre'), comment = c(ORCID = '0000-0002-3386-6076')), @@ -57,6 +57,6 @@ Suggests: Config/testthat/edition: 3 VignetteBuilder: knitr Encoding: UTF-8 -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.2 Roxygen: list(markdown = TRUE) Language: en-US diff --git a/R/cell_spec.R b/R/cell_spec.R index 760eee6..496bb66 100644 --- a/R/cell_spec.R +++ b/R/cell_spec.R @@ -38,6 +38,7 @@ #' tooltip and popover. #' @param new_tab T/F for whether to open up the new link in new tab. #' @param extra_css Extra css text to be passed into the cell +#' @param class Optional HTML class to apply to the cell #' @param escape T/F value showing whether special characters should be escaped. #' @param background_as_tile T/F value indicating if you want to have round #' cornered tile as background in HTML. @@ -46,6 +47,10 @@ #' background only works in a table cell. If it's `FALSE`, it works outside of a #' table environment. #' +#' @examples +#' cell_spec("The Title", format = "html", class = "title") +#' cell_spec("The Title", format = "latex", underline = TRUE) +#' #' @export cell_spec <- function(x, format, bold = FALSE, italic = FALSE, monospace = FALSE, @@ -54,6 +59,7 @@ cell_spec <- function(x, format, align = NULL, font_size = NULL, angle = NULL, tooltip = NULL, popover = NULL, link = NULL, new_tab = FALSE, extra_css = NULL, + class = NULL, escape = TRUE, background_as_tile = TRUE, latex_background_in_cell = TRUE) { @@ -69,7 +75,7 @@ cell_spec <- function(x, format, return(cell_spec_html(x, bold, italic, monospace, underline, strikeout, color, background, align, font_size, angle, tooltip, popover, link, new_tab, extra_css, - escape, background_as_tile)) + class, escape, background_as_tile)) } if (tolower(format) == "latex") { return(cell_spec_latex(x, bold, italic, monospace, underline, strikeout, @@ -80,7 +86,7 @@ cell_spec <- function(x, format, cell_spec_html <- function(x, bold, italic, monospace, underline, strikeout, color, background, align, font_size, angle, - tooltip, popover, link, new_tab, extra_css, + tooltip, popover, link, new_tab, extra_css, class, escape, background_as_tile) { if (escape) x <- escape_html(x) cell_style <- NULL @@ -107,6 +113,9 @@ cell_spec_html <- function(x, bold, italic, monospace, underline, strikeout, if (!is.null(extra_css)) { cell_style <- paste0(cell_style, extra_css) } + if (!is.null(class)) { + class <- paste0(' class="', class, '" ') + } if (!is.null(align)) { cell_style <- paste0(cell_style, "text-align: ", align, ";") } @@ -126,17 +135,23 @@ cell_spec_html <- function(x, bold, italic, monospace, underline, strikeout, tooltip_n_popover <- NULL } + if (!all(grepl("^ *$", cell_style))) { + cell_style <- paste0('style="', cell_style, '" ') + } else { + cell_style <- NULL + } + if (!is.null(link)) { if (new_tab) { target_blank = 'target="_blank" ' } else { target_blank = NULL } - x <- paste0('', x, '') + x <- paste0('', x, '') } else { - x <- paste0('', x, '') + x <- paste0('', x, '') } # if (!is.null(link)) { diff --git a/inst/NEWS.md b/inst/NEWS.md index 46066e6..91d760a 100644 --- a/inst/NEWS.md +++ b/inst/NEWS.md @@ -1,10 +1,11 @@ -kableExtra 1.4.0.7 +kableExtra 1.4.0.8 -------------------------------------------------------------------------------- New Features: * Added `show_every_page` argument to `footnote()` (#867). +* Added `class` argument to `cell_spec()` (#871). Bug Fixes: diff --git a/man/cell_spec.Rd b/man/cell_spec.Rd index 4358c78..8ee95ab 100644 --- a/man/cell_spec.Rd +++ b/man/cell_spec.Rd @@ -23,6 +23,7 @@ cell_spec( link = NULL, new_tab = FALSE, extra_css = NULL, + class = NULL, escape = TRUE, background_as_tile = TRUE, latex_background_in_cell = TRUE @@ -105,6 +106,8 @@ tooltip and popover.} \item{extra_css}{Extra css text to be passed into the cell} +\item{class}{Optional HTML class to apply to the cell} + \item{escape}{T/F value showing whether special characters should be escaped.} \item{background_as_tile}{T/F value indicating if you want to have round @@ -118,3 +121,8 @@ table environment.} \description{ Specify Cell format before it gets into kable } +\examples{ +cell_spec("The Title", format = "html", class = "title") +cell_spec("The Title", format = "latex", underline = TRUE) + +}