Skip to content

Commit

Permalink
bootstrap lines
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 25, 2024
1 parent 246e3c7 commit 6af1d70
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
15 changes: 14 additions & 1 deletion R/style_bootstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ style_bootstrap <- function(x,
fontsize = NULL,
width = NULL,
align = NULL,
line = NULL,
line_color = "black",
line_width = .1,
colspan = NULL,
indent = 0,
bootstrap_class = "table",
bootstrap_css = NULL,
bootstrap_css_rule = NULL) {
bootstrap_css_rule = NULL,
...) {


if (meta(x, "output") != "html") return(x)
Expand Down Expand Up @@ -96,6 +100,15 @@ style_bootstrap <- function(x,
settings$bootstrap <- paste(settings$bootstrap, sprintf("padding-left: %sem;", indent), sep = "")
}

if (!is.null(line)) {
settings$bootstrap <- vectorize_bootstrap(settings$bootstrap, line_color, "border-color: %s;")
}

if (grepl("t", line)) settings$bootstrap <- vectorize_bootstrap(settings$bootstrap, ".1em", "border-top: %s solid;")
if (grepl("b", line)) settings$bootstrap <- vectorize_bootstrap(settings$bootstrap, ".1em", "border-bottom: %s solid;")
if (grepl("l", line)) settings$bootstrap <- vectorize_bootstrap(settings$bootstrap, ".1em", "border-left: %s solid;")
if (grepl("r", line)) settings$bootstrap <- vectorize_bootstrap(settings$bootstrap, ".1em", "border-right: %s solid;")

# unique IDs for each CSS style combination
id <- sapply(unique(settings$bootstrap), function(k) get_id(stem = "tinytable_css_"))
settings$id <- id[match(settings$bootstrap, names(id))]
Expand Down
3 changes: 2 additions & 1 deletion R/style_tabularray.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ style_tabularray <- function(x,
colspan = NULL,
indent = 0,
tabularray_inner = NULL,
tabularray_outer = NULL) {
tabularray_outer = NULL,
...) {

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

Expand Down
41 changes: 38 additions & 3 deletions R/style_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
#' @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 line String determines if solid lines (rules or borders) should be drawn around the cell, row, or column.
#' + "t": top
#' + "b": bottom
#' + "l": left
#' + "r": right
#' + Can be combined such as: "lbt" to draw borders at the left, bottom, and top.
#' @param line_color Color of the line. See the `color` argument for details.
#' @param line_width Width of the line in em units (default: 0.1).
#' @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.
Expand Down Expand Up @@ -61,6 +69,9 @@ style_tt <- function (x,
align = NULL,
colspan = NULL,
indent = 0,
line = NULL,
line_color = "black",
line_width = 0.1,
tabularray_inner = NULL,
tabularray_outer = NULL,
bootstrap_class = "table",
Expand All @@ -86,6 +97,9 @@ style_tt <- function (x,
align = align,
colspan = colspan,
indent = indent,
line = line,
line_color = line_color,
line_width = line_width,
tabularray_inner = tabularray_inner,
tabularray_outer = tabularray_outer,
bootstrap_class = bootstrap_class,
Expand Down Expand Up @@ -118,6 +132,9 @@ style_tt_lazy <- function (x,
align,
colspan,
indent,
line,
line_color,
line_width,
tabularray_inner,
tabularray_outer,
bootstrap_class,
Expand Down Expand Up @@ -169,23 +186,27 @@ style_tt_lazy <- function (x,
assert_style_tt(
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,
line = line, line_color = line_color, line_width = line_width,
tabularray_inner = tabularray_inner, tabularray_outer = tabularray_outer, bootstrap_css = bootstrap_css,
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,
color = color, background = background, fontsize = fontsize, width = width, align = align, colspan = colspan, indent = indent,
tabularray_inner = tabularray_inner, tabularray_outer = tabularray_outer)
tabularray_inner = tabularray_inner, tabularray_outer = tabularray_outer,
line = line, line_color = line_color, line_width = line_width)

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_class = bootstrap_class)
bootstrap_css = bootstrap_css, bootstrap_css_rule = bootstrap_css_rule, bootstrap_class = bootstrap_class,
line = line, line_color = line_color, line_width = line_width)

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)
bootstrap_css = bootstrap_css, bootstrap_css_rule = bootstrap_css_rule,
line = line, line_color = line_color, line_width = line_width)

return(out)
}
Expand All @@ -206,6 +227,9 @@ assert_style_tt <- function (x,
align,
colspan,
indent,
line,
line_color,
line_width,
tabularray_inner,
tabularray_outer,
bootstrap_class = NULL,
Expand All @@ -225,10 +249,21 @@ assert_style_tt <- function (x,
assert_logical(monospace)
assert_logical(underline)
assert_logical(strikeout)
assert_string(line, null.ok = TRUE)
assert_string(line_color, null.ok = FALSE) # black default
assert_numeric(line_width, len = 1, lower = 0, null.ok = FALSE) # 0.1 default
assert_character(bootstrap_class, null.ok = TRUE)
assert_character(bootstrap_css, null.ok = TRUE)
assert_string(bootstrap_css_rule, null.ok = TRUE)

if (is.character(line)) {
line <- strsplit(line, split = "")[[1]]
if (!all(line %in% c("t", "b", "l", "r"))) {
msg <- "`line` must be a string of characters t, b, l, or r."
stop(msg, call. = FALSE)
}
}

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

Expand Down

0 comments on commit 6af1d70

Please sign in to comment.