Skip to content

Commit

Permalink
alignv argument in style_tt()
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Feb 9, 2024
1 parent 930295b commit 2be7874
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: tinytable
Type: Package
Title: Simple and Configurable Tables in 'HTML', 'LaTeX', 'Markdown', 'Word', 'PNG', 'PDF', and 'Typst' Formats
Description: Create highly customized tables with this simple and dependency-free package. Data frames can be converted to 'HTML', 'LaTeX', 'Markdown', 'Word', 'PNG', 'PDF', or 'Typst' tables. The user interface is minimalist and easy to learn. The syntax concise. 'HTML' tables can be customized using the flexible 'Bootstrap' framework, and 'LaTeX' code with the 'tabularray' package.
Version: 0.0.3.9006
Version: 0.0.3.9007
Depends:
R (>= 4.1.0)
Enhances:
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ New:
- Line plots: `plot_tt(fun = "line")`
- `format_tt(j=NULL, escape=TRUE)` now escapes column headers in addition to all cells.
- `style_tt()`: `rowspan` and `colspan` arguments are now supported for LaTeX and HTML tables.
- `style_tt()`: `alignv` argument is now supported for LaTeX and HTML

Bugfix:

Expand Down
12 changes: 12 additions & 0 deletions R/style_bootstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ style_bootstrap <- function(x,
fontsize = NULL,
width = NULL,
align = NULL,
alignv = NULL,
line = NULL,
line_color = "black",
line_width = .1,
Expand Down Expand Up @@ -81,6 +82,15 @@ style_bootstrap <- function(x,
align_bootstrap <- align
}

if (!is.null(alignv)) {
alignv_bootstrap <- switch(alignv,
"t" = "top",
"m" = "middle",
"b" = "bottom")
} else {
alignv_bootstrap <- alignv
}

if (!is.null(fontsize)) {
fontsize_bootstrap <- sprintf("%sem", fontsize)
} else {
Expand All @@ -95,6 +105,7 @@ style_bootstrap <- function(x,
settings$bootstrap <- vectorize_bootstrap(settings$bootstrap, monospace, "font-family: monospace;")
settings$bootstrap <- vectorize_bootstrap(settings$bootstrap, fontsize_bootstrap, "font-size: %s;")
settings$bootstrap <- vectorize_bootstrap(settings$bootstrap, align_bootstrap, "text-align: %s;")
settings$bootstrap <- vectorize_bootstrap(settings$bootstrap, alignv_bootstrap, "vertical-align: %s;")
settings$bootstrap <- vectorize_bootstrap(settings$bootstrap, color, "color: %s;")
settings$bootstrap <- vectorize_bootstrap(settings$bootstrap, background, "background-color: %s;")
settings$bootstrap <- vectorize_bootstrap(settings$bootstrap, width, "width: %s;")
Expand All @@ -120,6 +131,7 @@ style_bootstrap <- function(x,
listener <- "window.addEventListener('load', function () { spanCell_%s(%s, %s, %s, %s) })"
listener <- sprintf(listener, settings$id[1], settings$i[1], settings$j[1], rowspan, colspan)
out <- bootstrap_setting(out, listener, component = "cell")
listener <- "window.addEventListener('load', function () { styleCell_%s(%s, %s, %s, %s) })"
}

# CSS style for cell
Expand Down
10 changes: 10 additions & 0 deletions R/style_tabularray.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ style_tabularray <- function(x,
fontsize = NULL,
width = NULL,
align = NULL,
alignv = NULL,
line = NULL,
line_color = "black",
line_width = .1,
Expand Down Expand Up @@ -55,6 +56,15 @@ style_tabularray <- function(x,
span <- if (!is.null(colspan)) paste0(span, "c=", colspan, ",") else span
span <- if (!is.null(rowspan)) paste0(span, "r=", rowspan, ",") else span

if (!is.null(alignv)) {
alignv_tabularray <- switch(alignv,
"b" = "f",
"t" = "h",
"m" = "m"
)
settings$tabularray <- sprintf("%s valign=%s,", settings$tabularray, alignv_tabularray)
}

# convert to tabularray now that we've filled the bootstrap settings
if (is.numeric(fontsize)) settings$tabularray <- sprintf("%s font=\\fontsize{%sem}{%sem}\\selectfont,", settings$tabularray, fontsize, fontsize + 0.3)
if (!is.null(align)) settings$tabularray <- sprintf("%s halign=%s,", settings$tabularray, align)
Expand Down
21 changes: 15 additions & 6 deletions R/style_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#' @param width Width of column in em units. Can be `NULL` for default width.
#' @param fontsize Integer Font size in pt units.
#' @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 alignv A single character specifying vertical alignment. Valid characters include 't' (top), 'm' (middle), 'b' (bottom).
#' @param colspan Number of columns a cell should span. `i` and `j` must be of length 1.
#' @param rowspan Number of rows a cell should span. `i` and `j` must be of length 1.
#' @param indent Text indentation in em units. Positive values only.
Expand Down Expand Up @@ -80,6 +81,7 @@ style_tt <- function (x,
fontsize = NULL,
width = NULL,
align = NULL,
alignv = NULL,
colspan = NULL,
rowspan = NULL,
indent = 0,
Expand Down Expand Up @@ -109,6 +111,7 @@ style_tt <- function (x,
fontsize = fontsize,
width = width,
align = align,
alignv = alignv,
colspan = colspan,
rowspan = rowspan,
indent = indent,
Expand Down Expand Up @@ -145,6 +148,7 @@ style_tt_lazy <- function (x,
fontsize,
width,
align,
alignv,
colspan,
rowspan,
indent,
Expand All @@ -165,7 +169,10 @@ style_tt_lazy <- function (x,

j <- sanitize_j(j, x)

# align can be "c" or "clrrlc"takes many possible values
# alignv can only be a single character for now
assert_choice(alignv, c("t", "m", "b"), null.ok = TRUE)

# align can be "c" or "clrrlc" takes many possible values
assert_string(align, null.ok = TRUE)

if (!is.null(align)) {
Expand Down Expand Up @@ -198,7 +205,8 @@ 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, rowspan = rowspan, indent = indent,
color = color, background = background, fontsize = fontsize, width = width, align = align, alignv = alignv,
colspan = colspan, rowspan = rowspan, 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)
Expand All @@ -209,25 +217,25 @@ style_tt_lazy <- function (x,

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, rowspan = rowspan, indent = indent,
color = color, background = background, fontsize = fontsize, width = width, align = align, alignv = alignv, colspan = colspan, rowspan = rowspan, indent = indent,
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, rowspan = rowspan, indent = indent,
color = color, background = background, fontsize = fontsize, width = width, align = align, alignv = alignv, colspan = colspan, rowspan = rowspan, indent = indent,
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, rowspan = rowspan, indent = indent,
color = color, background = background, fontsize = fontsize, width = width, align = align, alignv = alignv, colspan = colspan, rowspan = rowspan, indent = indent,
bootstrap_css = bootstrap_css, bootstrap_css_rule = bootstrap_css_rule,
line = line, line_color = line_color, line_width = line_width)

out <- style_typst(
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, rowspan = rowspan, indent = indent,
color = color, background = background, fontsize = fontsize, width = width, align = align, alignv = alignv, colspan = colspan, rowspan = rowspan, indent = indent,
bootstrap_css = bootstrap_css, bootstrap_css_rule = bootstrap_css_rule,
line = line, line_color = line_color, line_width = line_width)

Expand All @@ -248,6 +256,7 @@ assert_style_tt <- function (x,
fontsize,
width,
align,
alignv,
colspan,
rowspan,
indent,
Expand Down
6 changes: 4 additions & 2 deletions inst/tinytest/test-colrowspan.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

# x <- mtcars[1:3, 1:3]

# pkgload::load_all()
# tt(x, placement = "H") |>
# style_tt(1, 1, rowspan = 2, colspan = 2, align = "c") |>
# style_tt(1, 1, rowspan = 2, colspan = 2, align = "l", alignv = "t") |>
# print("latex")

# pkgload::load_all()
# tt(x, placement = "H") |>
# style_tt(1, 1, rowspan = 2, colspan = 2, align = "c") |>
# style_tt(1, 1, rowspan = 2, colspan = 2, align = "r", alignv = "b") |>
# save_tt("~/Downloads/trash.html", overwrite = TRUE)
3 changes: 3 additions & 0 deletions man/style_tt.Rd

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

6 changes: 4 additions & 2 deletions vignettes/tutorial.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -539,16 +539,18 @@ tt(x) |> style_tt(j = "mpg|hp|qsec", fontsize = 1.5)

## Spanning cells

Sometimes, it can be useful to make a cell stretch across multiple colums, for example when we want to insert a label. To achieve this, we can use the `colspan` argument. Here, we make the 2nd cell of the 2nd row stretch across three columns:
Sometimes, it can be useful to make a cell stretch across multiple colums, for example when we want to insert a label. To achieve this, we can use the `colspan` argument. Here, we make the 2nd cell of the 2nd row stretch across three columns and two rows:

```{r}
tt(x)|> style_tt(
i = 2, j = 2,
colspan = 3,
rowspan = 2,
align = "c",
alignv = "m",
color = "white",
background = "black")
background = "black",
bold = TRUE)
```

Here is the original table for comparison:
Expand Down

0 comments on commit 2be7874

Please sign in to comment.