Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bootstrap lines #104

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 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,14 @@ style_bootstrap <- function(x,
settings$bootstrap <- paste(settings$bootstrap, sprintf("padding-left: %sem;", indent), sep = "")
}

if (!is.null(line)) {
tmp <- sprintf(": solid %s %s;", paste0(line_width, "em"), line_color)
if (grepl("t", line)) settings$bootstrap <- paste0(settings$bootstrap, " border-top", tmp)
if (grepl("b", line)) settings$bootstrap <- paste0(settings$bootstrap, " border-bottom", tmp)
if (grepl("l", line)) settings$bootstrap <- paste0(settings$bootstrap, " border-left", tmp)
if (grepl("r", line)) settings$bootstrap <- paste0(settings$bootstrap, " border-right", tmp)
}

# 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
61 changes: 51 additions & 10 deletions R/style_tabularray.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ style_tabularray <- function(x,
fontsize = NULL,
width = NULL,
align = NULL,
line = NULL,
line_color = "black",
line_width = .1,
colspan = NULL,
indent = 0,
tabularray_inner = NULL,
tabularray_outer = NULL) {
tabularray_outer = NULL,
...) {

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

Expand Down Expand Up @@ -102,15 +106,52 @@ style_tabularray <- function(x,
settings$tabularray <- trimws(gsub("\\s+", "", settings$tabularray))
settings$tabularray <- trimws(gsub(",+", ",", settings$tabularray))

for (k in seq_len(nrow(settings))) {
if (all(c("i", "j") %in% colnames(settings))) {
spec <- sprintf("cell{%s}{%s}={%s}{%s},", settings$i[k], settings$j[k], span, settings$tabularray[k])
} else if ("i" %in% colnames(settings)) {
spec <- sprintf("row{%s}={%s},", settings$i[k], settings$tabularray[k])
} else if ("j" %in% colnames(settings)) {
spec <- sprintf("column{%s}={%s},", settings$j[k], settings$tabularray[k])
}
out <- tabularray_insert(out, content = spec, type = "inner")

if (!all(settings$tabularray == ",")) {
for (k in seq_len(nrow(settings))) {
if (all(c("i", "j") %in% colnames(settings))) {
spec <- sprintf("cell{%s}{%s}={%s}{%s},", settings$i[k], settings$j[k], span, settings$tabularray[k])
} else if ("i" %in% colnames(settings)) {
spec <- sprintf("row{%s}={%s},", settings$i[k], settings$tabularray[k])
} else if ("j" %in% colnames(settings)) {
spec <- sprintf("column{%s}={%s},", settings$j[k], settings$tabularray[k])
}
out <- tabularray_insert(out, content = spec, type = "inner")
}
}

# Lines are not part of cellspec/rowspec/columnspec. Do this separately.
if (!is.null(line)) {
# TODO: handle hex colors
# browser()
iline <- jline <- NULL
if (grepl("t", line)) iline <- c(iline, ival + meta(x, "nhead"))
if (grepl("b", line)) iline <- c(iline, ival + meta(x, "nhead") + 1)
if (grepl("l", line)) jline <- c(jline, jval)
if (grepl("r", line)) jline <- c(jline, jval + 1)
iline <- unique(iline)
jline <- unique(jline)
line_width <- paste0(line_width, "em")
if (!is.null(iline)) {
tmp <- sprintf(
"hline{%s}={%s}{solid, %s, %s},",
paste(iline, collapse = ","),
paste(jval, collapse = ","),
line_width,
line_color
)
out <- tabularray_insert(out, content = tmp, type = "inner")
}
if (!is.null(jline)) {
tmp <- sprintf(
"vline{%s}={%s}{solid, %s, %s},",
paste(jline, collapse = ","),
paste(ival + meta(x, "nhead"), collapse = ","),
line_width,
line_color
)
out <- tabularray_insert(out, content = tmp, type = "inner")
}
}

out <- tabularray_insert(out, content = tabularray_inner, type = "inner")
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
6 changes: 3 additions & 3 deletions man/plot_tt.Rd

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

16 changes: 16 additions & 0 deletions man/style_tt.Rd

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

36 changes: 36 additions & 0 deletions vignettes/tutorial.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,42 @@ tt(k, width = .7, theme = "void") |>
fontsize = fs)
```


## Lines (borders)

The `style_tt` function allows us to customize the borders that surround eacell of a table, as well horizontal and vertical rules. To control these lines, we use the `line`, `line_width`, and `line_color` arguments. Here's a brief overview of each of these arguments:

- `line`: This argument specifies where solid lines should be drawn. It is a string that can consist of the following characters:
- `"t"`: Draw a line at the top of the cell, row, or column.
- `"b"`: Draw a line at the bottom of the cell, row, or column.
- `"l"`: Draw a line at the left side of the cell, row, or column.
- `"r"`: Draw a line at the right side of the cell, row, or column.
- You can combine these characters to draw lines on multiple sides, such as `"tbl"` to draw lines at the top, bottom, and left sides of a cell.
- `line_width`: This argument controls the width of the solid lines in em units (default: 0.1 em). You can adjust this value to make the lines thicker or thinner.
- `line_color`: Specifies the color of the solid lines. You can use color names, hexadecimal codes, or other color specifications to define the line color.

Here is an example where we draw lines around every border ("t", "b", "l", and "r") of specified cells.

```{r}
tt(x, theme = "void") |>
style_tt(
i = 0:3,
j = 1:3,
line = "tblr",
line_width = 0.4,
line_color = "orange")
```

And here is an example with horizontal rules:

```{r}
tt(x, theme = "void") |>
style_tt(i = 0, line = "t", line_color = "orange", line_width = 0.4) |>
style_tt(i = 0, line = "b", line_color = "purple", line_width = 0.2) |>
style_tt(i = 4, line = "b", line_color = "orange", line_width = 0.4)
```


# Tiny plots and images

The `plot_tt()` function can embed images and plots in a `tinytable`. We can insert images by specifying their paths and positions (`i`/`j`).
Expand Down
Loading