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

Align deprecate #77

Merged
merged 3 commits into from
Jan 18, 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
2 changes: 1 addition & 1 deletion R/style_tabularray.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ style_tabularray <- function(x,
# colspan requires cell level, so we keep the full settings DF
if (is.null(colspan)) {
if (is.null(i) && is.null(j)) {
settings <- unique(settings[, c("i", "tabularray"), drop = FALSE])
settings <- unique(settings[, c("j", "tabularray"), drop = FALSE])
} else if (is.null(i)) {
settings <- unique(settings[, c("j", "tabularray"), drop = FALSE])
} else if (is.null(j)) {
Expand Down
18 changes: 16 additions & 2 deletions R/style_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#' @param fontsize Font size. Can be `NULL` for default size.
#' @param width Width of the cell or column. Can be `NULL` for default width.
#' @param fontsize Integer Font size in pt units.
#' @param align Text alignment within the cell. Options are 'c' (center), 'l' (left), or 'r' (right). Can be `NULL` for default alignment.
#' @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 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`.
Expand Down Expand Up @@ -113,6 +113,21 @@ style_tt_lazy <- function (x,
j <- grep(j, meta(x, "colnames"), perl = TRUE)
}

# align can be "c" or "clrrlc"takes many possible values
assert_string(align, null.ok = TRUE)
nalign <- if (is.null(j)) meta(x, "ncols") else length(j)
if (!is.null(align)) {
align <- strsplit(align, split = "")[[1]]
if (length(align) != 1 && length(align) != nalign) {
msg <- sprintf("`align` must be a single character or a string of length %s.", nalign)
stop(msg, call. = FALSE)
}
if (any(!align %in% c("c", "l", "r"))) {
msg <- "`align` must be characters c, l, or r."
stop(msg, call. = FALSE)
}
}

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,
Expand Down Expand Up @@ -157,7 +172,6 @@ assert_style_tt <- function (x,

assert_integerish(colspan, len = 1, lower = 1, null.ok = TRUE)
assert_string(width, null.ok = TRUE)
assert_choice(align, c("c", "l", "r"), null.ok = TRUE)
assert_numeric(indent, len = 1, lower = 0)
assert_character(background, null.ok = TRUE)
assert_character(color, null.ok = TRUE)
Expand Down
21 changes: 0 additions & 21 deletions R/tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#' @param x A data frame or data table to be rendered as a table.
#' @param output The format of the output table. Can be "html", "latex", or "markdown". If NULL, the format is automatically detected in Quarto or Rmarkdown documents.
#' @param digits Number of significant digits to keep for numeric variables. When `digits` is an integer, `tt()` calls `format_tt(x, digits = digits)` before proceeding to draw the table. Users who need more control can proceed in two steps: (1) format the data with `format_tt()` or other functions, and (2) pass the formatted data to `tt()` for drawing. See `?format_tt` for more details on formating options (ex: decimal, scientific notation, dates, boolean variables, etc.).
#' @param align A string specifying the alignment of columns. Each character in the string corresponds to a column; 'l' for left, 'c' for center, and 'r' for right alignment. The length of the string must match the number of columns in `x`.
#' @param caption A string that will be used as the caption of the table.
#' @param width A numeric value between 0 and 1 indicating the proportion of the line width that the table should cover.
#' @param theme The theme to apply to the table.
Expand All @@ -27,14 +26,12 @@
#' tt(x,
#' theme = "striped",
#' width = 0.5,
#' align = "ccrrl",
#' caption = "Data about cars.")
#'
#' @export
tt <- function(x,
output = NULL,
digits = getOption("digits"),
align = NULL,
caption = NULL,
width = NULL,
notes = NULL,
Expand All @@ -45,7 +42,6 @@ tt <- function(x,
output <- sanitize_output(output)
assert_data_frame(x)
assert_string(caption, null.ok = TRUE)
assert_string(align, null.ok = TRUE)
assert_numeric(width, len = 1, lower = 0, upper = 1, null.ok = TRUE)
assert_integerish(digits, len = 1, null.ok = TRUE)

Expand Down Expand Up @@ -83,23 +79,6 @@ tt <- function(x,
out <- meta(out, "ncols", ncol(x))
out <- meta(out, "lazy_style", list())

if (!is.null(align)) {
if (nchar(align) != ncol(x)) {
msg <- sprintf("`align` must have length %s, equal to the number of columns in `x`.", ncol(x))
stop(msg, call. = FALSE)
}
align <- strsplit(align, split = "")[[1]]
if (!all(align %in% c("l", "c", "r"))) {
msg <- "Elements of `align` must be 'c', 'l', or 'r'."
stop(msg, call. = FALSE)
}
for (col in seq_along(align)) {
# cleaner code if we do it in two shots for tabularray
out <- style_tt(out, j = col, align = align[[col]])
out <- style_tt(out, i = 0, j = col, align = align[[col]])
}
}

# placement
assert_string(placement, null.ok = TRUE)
if (!is.null(placement)) {
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@


<p align="center">
<img src="man/figures/tinytable_logo.svg" height = "250" class = "center">
</p>
Expand Down
13 changes: 0 additions & 13 deletions README.qmd
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@














<p align="center">
<img src="man/figures/tinytable_logo.svg" height = "250" class = "center">
</p>
Expand Down
2 changes: 1 addition & 1 deletion man/style_bootstrap.Rd

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

2 changes: 1 addition & 1 deletion man/style_tt.Rd

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

4 changes: 0 additions & 4 deletions man/tt.Rd

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

24 changes: 17 additions & 7 deletions vignettes/tutorial.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: "`tinytable`"
subtitle: "Easy, beautiful, and customizable tables in R"
format:
html: default
pdf:
Expand Down Expand Up @@ -95,10 +96,17 @@ tt(x, theme = "void")

## Alignment

To align columns, we use a single string, where each letter represents a column:
To align columns, we use a single character, or a string where each letter represents a column:

```{r}
tt(x, align = "ccrrl")
dat <- data.frame(
a = c("a", "aa", "aaa"),
b = c("b", "bb", "bbb"),
c = c("c", "cc", "ccc"))

tt(dat) |> style_tt(align = "c")

tt(dat) |> style_tt(j = 1:3, align = "lcr")
```

## Formatting (numbers, dates, strings, etc.)
Expand Down Expand Up @@ -253,7 +261,7 @@ In LaTeX and MathJax (for HTML), there are two main ways to enclose mathematical

```{r}
dat <- data.frame(Math = c("\\( x^2 + y^2 = z^2 \\)", "\\( \\frac{1}{2} \\)"))
tt(dat, align = "c")
tt(dat) |> style_tt(align = "c")
```

::: {.content-visible when-format="pdf"}
Expand All @@ -262,7 +270,8 @@ In LaTeX (PDF), you can also use the `mode` inner setting from `tabularray` to r
```{r, eval = knitr::is_latex_output()}

dat <- data.frame(Math = c("x^2 + y^2 = z^2", "\\frac{1}{2}"))
tt(dat, align = "c") |> style_tt(tabularray_inner = "column{1}={mode=math},")
tt(dat) |>
style_tt(align = "c", tabularray_inner = "column{1}={mode=math},")
```
:::

Expand Down Expand Up @@ -464,7 +473,8 @@ bg <- hcl.colors(20, "Inferno")
fg <- ifelse(as.matrix(k) < 17, tail(bg, 1), head(bg, 1))
fs <- 1:20

tt(k, width = .5, theme = "void", align = "ccccc") |>
tt(k, width = .5, theme = "void") |>
style_tt(align = "ccccc") |>
style_tt(
i = 1:4,
j = 1:5,
Expand Down Expand Up @@ -625,8 +635,8 @@ css_rule <- "
}
"

tt(x, align = "ccccc", theme = "table mytable", width = 2/3) |>
style_tt(bootstrap_css_rule = css_rule)
tt(x, theme = "table mytable", width = 2/3) |>
style_tt(align = "ccccc", bootstrap_css_rule = css_rule)
```

:::
Expand Down
Loading