Skip to content

Commit

Permalink
word bold italic, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 20, 2024
1 parent cb03657 commit ad71598
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
17 changes: 14 additions & 3 deletions R/build_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ build_tt <- function(x, output = NULL) {
l[["x"]] <- tmp
out <- eval(l)
}

# markdown styles need to be applied before creating the table, otherwise there's annoying parsing, etc.
if (output == "markdown") {
for (l in m$lazy_style) {
l[["x"]] <- out
out <- eval(l)
}
}

# shouldn't have to add this everywhere, but I'm too lazy to check
out <- meta(out, "output", output)

Expand Down Expand Up @@ -46,9 +55,11 @@ build_tt <- function(x, output = NULL) {
out <- meta(out, "output", output)

# style the table
for (l in m$lazy_style) {
l[["x"]] <- out
out <- eval(l)
if (output != "markdown") {
for (l in m$lazy_style) {
l[["x"]] <- out
out <- eval(l)
}
}
out <- meta(out, "output", output)

Expand Down
40 changes: 40 additions & 0 deletions R/style_grid.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
style_grid <- function(x,
i = NULL,
j = NULL,
bold = FALSE,
italic = FALSE,
monospace = FALSE,
underline = FALSE,
strikeout = FALSE,
...) {

if (meta(x, "output") != "markdown") return(x)

out <- x

ival <- if (is.null(i)) seq_len(meta(x, "nrows")) else i
jval <- if (is.null(j)) seq_len(meta(x, "ncols")) else j

for (col in seq_along(out)) {
out[[col]] <- as.character(out[[col]])
}

for (row in ival) {
for (col in jval) {
if (isTRUE(bold)) {
out[row, col] <- sprintf("**%s**", out[row, col])
}
if (isTRUE(italic)) {
out[row, col] <- sprintf("*%s*", out[row, col])
}
if (isTRUE(strikeout)) {
out[row, col] <- sprintf("~~%s~~", out[row, col])
}
}
}

attr(out, "tinytable_meta") <- meta(x)
class(out) <- class(x)
return(out)
}

10 changes: 9 additions & 1 deletion R/style_tt.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#' Style a Tiny Table in either LaTeX or HTML format
#'
#' @details
#' This function applies styling to a table created by `tt()`. It allows customization of text style (bold, italic, monospace), text and background colors, font size, cell width, text alignment, column span, and indentation. The function supports both LaTeX (tabularray) and HTML (bootstrap) formats.
#'
#' Note that Markdown and Word formats are limited to these styles: italic, bold, strikeout.
#'
#' @param x A table object created by `tt()`.
#' @param i Row indices where the styling should be applied. Can be a single value or a vector. If `colspan` is used, `i` must be of length 1. When i=0, the header is styled.
#' @param j Column indices where the styling should be applied. Can be a single value, a vector, or a Perl-style regular expression applied to column names of the original data frame. If `colspan` is used, `j` must be of length 1.
Expand Down Expand Up @@ -119,7 +122,7 @@ style_tt_lazy <- function (x,

# sanity x
if (is.null(meta(x))) stop("`x` must be generated by `tinytable::tt()`.", call. = FALSE)
if (!isTRUE(meta(x)$output %in% c("html", "latex"))) return(x)
if (!isTRUE(meta(x)$output %in% c("html", "latex", "markdown"))) return(x)

out <- x

Expand Down Expand Up @@ -175,6 +178,11 @@ style_tt_lazy <- function (x,
color = color, background = background, fontsize = fontsize, width = width, align = align, colspan = colspan, indent = indent,
bootstrap_css = bootstrap_css, bootstrap_css_rule = bootstrap_css_rule)

out <- style_grid(
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)

return(out)
}

Expand Down
5 changes: 5 additions & 0 deletions man/style_tt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ad71598

Please sign in to comment.