diff --git a/R/style_tabularray.R b/R/style_tabularray.R index a88711d7..5d2f7f55 100644 --- a/R/style_tabularray.R +++ b/R/style_tabularray.R @@ -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)) { diff --git a/R/style_tt.R b/R/style_tt.R index e944010a..9ef92226 100644 --- a/R/style_tt.R +++ b/R/style_tt.R @@ -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`. @@ -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, @@ -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) diff --git a/R/tt.R b/R/tt.R index 96cd75d8..6e563362 100644 --- a/R/tt.R +++ b/R/tt.R @@ -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. @@ -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, @@ -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) @@ -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)) { diff --git a/README.md b/README.md index ce99fe2d..e8a1e999 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ +

diff --git a/README.qmd b/README.qmd index 00f203c3..bd7d4bbb 100644 --- a/README.qmd +++ b/README.qmd @@ -1,17 +1,4 @@ - - - - - - - - - - - - -

diff --git a/man/style_bootstrap.Rd b/man/style_bootstrap.Rd index cd62b760..7513d7a2 100644 --- a/man/style_bootstrap.Rd +++ b/man/style_bootstrap.Rd @@ -63,7 +63,7 @@ style_bootstrap( \item{width}{Width of the cell or column. Can be \code{NULL} for default width.} -\item{align}{Text alignment within the cell. Options are 'c' (center), 'l' (left), or 'r' (right). Can be \code{NULL} for default alignment.} +\item{align}{A single character or a string with a number of characters equal to the number of columns in \code{j}. Valid characters include 'c' (center), 'l' (left), or 'r' (right).} \item{colspan}{Number of columns a cell should span. Can only be used if both \code{i} and \code{j} are of length 1. Must be an integer greater than 1.} diff --git a/man/style_tt.Rd b/man/style_tt.Rd index db628dde..2dfe5432 100644 --- a/man/style_tt.Rd +++ b/man/style_tt.Rd @@ -65,7 +65,7 @@ style_tt( \item{width}{Width of the cell or column. Can be \code{NULL} for default width.} -\item{align}{Text alignment within the cell. Options are 'c' (center), 'l' (left), or 'r' (right). Can be \code{NULL} for default alignment.} +\item{align}{A single character or a string with a number of characters equal to the number of columns in \code{j}. Valid characters include 'c' (center), 'l' (left), or 'r' (right).} \item{colspan}{Number of columns a cell should span. Can only be used if both \code{i} and \code{j} are of length 1. Must be an integer greater than 1.} diff --git a/man/tt.Rd b/man/tt.Rd index f7074bc1..8a5b2965 100644 --- a/man/tt.Rd +++ b/man/tt.Rd @@ -8,7 +8,6 @@ tt( x, output = NULL, digits = getOption("digits"), - align = NULL, caption = NULL, width = NULL, notes = NULL, @@ -23,8 +22,6 @@ tt( \item{digits}{Number of significant digits to keep for numeric variables. When \code{digits} is an integer, \code{tt()} calls \code{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 \code{format_tt()} or other functions, and (2) pass the formatted data to \code{tt()} for drawing. See \code{?format_tt} for more details on formating options (ex: decimal, scientific notation, dates, boolean variables, etc.).} -\item{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 \code{x}.} - \item{caption}{A string that will be used as the caption of the table.} \item{width}{A numeric value between 0 and 1 indicating the proportion of the line width that the table should cover.} @@ -72,7 +69,6 @@ tt(x) tt(x, theme = "striped", width = 0.5, - align = "ccrrl", caption = "Data about cars.") } diff --git a/vignettes/tutorial.qmd b/vignettes/tutorial.qmd index 07e2e6bf..a793a8ad 100644 --- a/vignettes/tutorial.qmd +++ b/vignettes/tutorial.qmd @@ -1,5 +1,6 @@ --- title: "`tinytable`" +subtitle: "Easy, beautiful, and customizable tables in R" format: html: default pdf: @@ -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.) @@ -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"} @@ -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},") ``` ::: @@ -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, @@ -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) ``` :::