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

Stricter (and less automagic) format_tt() #406

Merged
merged 7 commits into from
Dec 13, 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
3 changes: 1 addition & 2 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ README.qmd
man-roxygen
Makefile
.quarto
vignettes/.*\.qmd$
vignettes/.*\.bib$
vignettes/
docs/
docs
.github
Expand Down
104 changes: 49 additions & 55 deletions R/format_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#' @param fn Function for custom formatting. Accepts a vector and returns a character vector of the same length.
#' @param quarto Logical. Enable Quarto data processing and wrap cell content in a `data-qmd` span (HTML) or `\QuartoMarkdownBase64{}` macro (LaTeX). See warnings in the Global Options section below.
#' @param sprintf String passed to the `?sprintf` function to format numbers or interpolate strings with a user-defined pattern (similar to the `glue` package, but using Base R).
#' @param ... Additional arguments are ignored.
#' @inheritParams tt
#' @inheritParams style_tt
#' @template global_options
Expand Down Expand Up @@ -85,24 +84,34 @@ format_tt <- function(x,
num_suffix = get_option("tinytable_format_num_suffix", default = FALSE),
num_mark_big = get_option("tinytable_format_num_mark_big", default = ""),
num_mark_dec = get_option("tinytable_format_num_mark_dec", default = getOption("OutDec", default = ".")),
date = get_option("tinytable_format_date", default = "%Y-%m-%d"),
bool = get_option("tinytable_format_bool", default = function(column) tools::toTitleCase(tolower(column))),
date = get_option("tinytable_format_date", default = NULL),
bool = get_option("tinytable_format_bool", default = NULL),
math = get_option("tinytable_format_math", default = FALSE),
other = get_option("tinytable_format_other", default = as.character),
replace = get_option("tinytable_format_replace", default = TRUE),
other = get_option("tinytable_format_other", default = NULL),
replace = get_option("tinytable_format_replace", default = FALSE),
escape = get_option("tinytable_format_escape", default = FALSE),
markdown = get_option("tinytable_format_markdown", default = FALSE),
quarto = get_option("tinytable_format_quarto", default = FALSE),
fn = get_option("tinytable_format_fn", default = NULL),
sprintf = get_option("tinytable_format_sprintf", default = NULL),
...) {
out <- x
sprintf = get_option("tinytable_format_sprintf", default = NULL)) {

dots <- list(...)
if ("replace_na" %in% names(dots)) {
replace <- dots[["replace_na"]]
warning("The `replace_na` argument was renamed `replace`.", call. = FALSE)
}
assert_integerish(digits, len = 1, null.ok = TRUE)
assert_choice(num_fmt, c("significant", "significant_cell", "decimal", "scientific"))
assert_flag(num_zero)
assert_string(num_mark_big)
assert_string(num_mark_dec)
assert_string(date, null.ok = TRUE)
assert_function(bool, null.ok = TRUE)
assert_function(identity, null.ok = TRUE)
assert_function(other, null.ok = TRUE)
assert_flag(markdown)
assert_flag(quarto)
assert_function(fn, null.ok = TRUE)
assert_string(sprintf, null.ok = TRUE)
replace <- sanitize_replace(replace)
sanity_num_mark(digits, num_mark_big, num_mark_dec)

out <- x

if (inherits(out, "tinytable")) {
cal <- call("format_tt_lazy",
Expand Down Expand Up @@ -159,27 +168,27 @@ format_tt <- function(x,
}

format_tt_lazy <- function(x,
i = NULL,
j = NULL,
i,
j,
digits,
num_fmt = "significant",
num_zero = FALSE,
num_suffix = FALSE,
num_mark_big = "",
num_mark_dec = NULL,
replace = TRUE,
fn = NULL,
sprintf = NULL,
url = FALSE,
date = "%Y-%m-%d",
bool = identity,
math = FALSE,
escape = FALSE,
markdown = FALSE,
quarto = quarto,
other = as.character,
inull = FALSE,
jnull = FALSE) {
num_fmt,
num_zero,
num_suffix,
num_mark_big,
num_mark_dec,
replace,
fn,
sprintf,
url,
date,
bool,
math,
escape,
markdown,
quarto,
other,
inull,
jnull) {
# format_tt() supports vectors
if (isTRUE(check_atomic_vector(x))) {
atomic_vector <- TRUE
Expand All @@ -198,21 +207,6 @@ format_tt_lazy <- function(x,
stop("`x` must be a `tinytable` object, a data frame, or an atomic vector.", call. = FALSE)
}

assert_integerish(digits, len = 1, null.ok = TRUE)
assert_integerish(i, null.ok = TRUE)
assert_choice(num_fmt, c("significant", "significant_cell", "decimal", "scientific"))
assert_flag(num_zero)
assert_string(num_mark_big)
assert_string(num_mark_dec)
assert_string(date)
assert_function(bool)
assert_function(identity)
assert_function(fn, null.ok = TRUE)
assert_string(sprintf, null.ok = TRUE)
assert_flag(markdown)
assert_flag(quarto)
replace <- sanitize_replace(replace)
sanity_num_mark(digits, num_mark_big, num_mark_dec)

i <- sanitize_i(i, x, lazy = FALSE)
j <- sanitize_j(j, x)
Expand All @@ -230,15 +224,15 @@ format_tt_lazy <- function(x,
out[i, col] <- base::sprintf(sprintf, ori[i, col, drop = TRUE])
} else {
# logical
if (is.logical(ori[i, col])) {
if (!is.null(bool) && is.logical(ori[i, col])) {
out[i, col] <- bool(ori[i, col, drop = TRUE])

# date
} else if (inherits(ori[i, col], "Date")) {
# date
} else if (!is.null(date) && inherits(ori[i, col], "Date")) {
out[i, col] <- format(ori[i, col, drop = TRUE], date)

# numeric
} else if (is.numeric(ori[i, col, drop = TRUE])) {
# numeric
} else if (!is.null(digits) && is.numeric(ori[i, col, drop = TRUE])) {
tmp <- format_numeric(ori[i, col],
num_suffix = num_suffix,
digits = digits,
Expand All @@ -249,8 +243,8 @@ format_tt_lazy <- function(x,
)
if (!is.null(tmp)) out[i, col] <- tmp

# other
} else {
# other
} else if (is.function(other)) {
out[i, col] <- other(ori[i, col, drop = TRUE])
}
}
Expand Down
19 changes: 19 additions & 0 deletions R/sanity.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ sanity_align <- function(align, i) {
}


sanitize_i <- function(i, x, pre_group_i = FALSE, lazy = TRUE) {
out <- seq_len(nrow(x))
if (is.null(i) && isTRUE(lazy)) {
out <- NA
attr(out, "null") <- TRUE
attr(out, "body") <- seq_len(nrow(x))
attr(out, "head") <- integer()
} else {
if (!is.null(i)) {
out <- i
} else if (inherits(x, "tinytable")) {
out <- seq_len(nrow(x@table_dataframe))
}
attr(out, "null") <- FALSE
attr(out, "body") <- out[out > 0]
attr(out, "head") <- out[out < 1]
}
return(out)
}
sanitize_i <- function(i, x, pre_group_i = FALSE, lazy = TRUE) {
if (is.character(i)) {
assert_choice(i, c("notes", "caption"))
Expand Down
2 changes: 1 addition & 1 deletion R/save_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ latex_standalone <- "
\\usepackage{rotating}
\\usepackage{float}
\\usepackage[normalem]{ulem}
\\usepackage[HTML]{xcolor}
\\usepackage[x11names, svgnames]{xcolor}
\\UseTblrLibrary{booktabs}
\\UseTblrLibrary{siunitx}
\\newcommand{\\tinytableTabularrayUnderline}[1]{\\underline{#1}}
Expand Down
13 changes: 0 additions & 13 deletions R/style_string.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,6 @@ style_string_typst <- function(n, styles) {
}



style_notes <- function(x) {
fun <- switch(x@output,
"typst" = style_string_typst,
"html" = style_string_html,
"html_portable" = style_string_html,
"latex" = style_string_latex,
function(k, ...) identity(k)
)
x@notes <- lapply(x@notes, fun, x@style_notes)
return(x)
}

style_notes <- function(x) {
fun <- switch(x@output,
"typst" = style_string_typst,
Expand Down
1 change: 1 addition & 0 deletions R/tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ tt <- function(x,
# twice because format() leaves Date type, which cannot be partially reasigned
# with indexed format_tt(i)
tab <- data.frame(lapply(tab, format))
tab <- data.frame(lapply(tab, trimws))
colnames(tab) <- colnames(x)

out <- methods::new("tinytable",
Expand Down
40 changes: 20 additions & 20 deletions altdoc/quarto_website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@ website:
contents:
- section: Tutorial
contents:
- text: Tiny tables
file: vignettes/tinytable.qmd
- text: Format
file: vignettes/format.qmd
- text: Style
file: vignettes/style.qmd
- text: Group labels
file: vignettes/group.qmd
- text: Plots and images
file: vignettes/plot.qmd
- text: Themes
file: vignettes/theme.qmd
- text: Customization
file: vignettes/custom.qmd
- text: Notebooks
file: vignettes/notebooks.qmd
- text: FAQ
file: vignettes/faq.qmd
- text: Alternatives
file: vignettes/alternatives.qmd
- text: Tiny tables
file: vignettes/tinytable.qmd
- text: Format
file: vignettes/format.qmd
- text: Style
file: vignettes/style.qmd
- text: Group labels
file: vignettes/group.qmd
- text: Plots and images
file: vignettes/plot.qmd
- text: Themes
file: vignettes/theme.qmd
- text: Customization
file: vignettes/custom.qmd
- text: Notebooks
file: vignettes/notebooks.qmd
- text: FAQ
file: vignettes/faq.qmd
- text: Alternatives
file: vignettes/alternatives.qmd
- text: "Tutorial (PDF)"
file: vignettes/tinytable_tutorial.pdf
- section: Functions
Expand Down
2 changes: 1 addition & 1 deletion inst/tinytest/_tinysnapshot/docx-issue98_01.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
+------+-----+------+-----+------+
| 21.0 | 6 | 160 | 110 | 3.90 |
+------+-----+------+-----+------+
| 22.8 | 4 | 108 | 93 | 3.85 |
| 22.8 | 4 | 108 | 93 | 3.85 |
+------+-----+------+-----+------+
| 21.4 | 6 | 258 | 110 | 3.08 |
+------+-----+------+-----+------+
22 changes: 11 additions & 11 deletions inst/tinytest/_tinysnapshot/docx-issue98_02.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

+-----------+-----+------+-----+------+
| mpg | cyl | disp | hp | drat |
+===========+=====+======+=====+======+
| $\sigma$ | 6 | 160 | 110 | 3.90 |
+-----------+-----+------+-----+------+
| 21 | 6 | 160 | 110 | 3.90 |
+-----------+-----+------+-----+------+
| 22.8 | 4 | 108 | 93 | 3.85 |
+-----------+-----+------+-----+------+
| 21.4 | 6 | 258 | 110 | 3.08 |
+-----------+-----+------+-----+------+
+----------+-----+------+-----+------+
| mpg | cyl | disp | hp | drat |
+==========+=====+======+=====+======+
| $\sigma$ | 6 | 160 | 110 | 3.90 |
+----------+-----+------+-----+------+
| 21 | 6 | 160 | 110 | 3.90 |
+----------+-----+------+-----+------+
| 22.8 | 4 | 108 | 93 | 3.85 |
+----------+-----+------+-----+------+
| 21.4 | 6 | 258 | 110 | 3.08 |
+----------+-----+------+-----+------+
22 changes: 11 additions & 11 deletions inst/tinytest/_tinysnapshot/docx-issue98_03.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

+-----------+-----+------+-----+------+
| $\sigma$ | cyl | disp | hp | drat |
+===========+=====+======+=====+======+
| $\sigma$ | 6 | 160 | 110 | 3.90 |
+-----------+-----+------+-----+------+
| 21 | 6 | 160 | 110 | 3.90 |
+-----------+-----+------+-----+------+
| 22.8 | 4 | 108 | 93 | 3.85 |
+-----------+-----+------+-----+------+
| 21.4 | 6 | 258 | 110 | 3.08 |
+-----------+-----+------+-----+------+
+----------+-----+------+-----+------+
| $\sigma$ | cyl | disp | hp | drat |
+==========+=====+======+=====+======+
| $\sigma$ | 6 | 160 | 110 | 3.90 |
+----------+-----+------+-----+------+
| 21 | 6 | 160 | 110 | 3.90 |
+----------+-----+------+-----+------+
| 22.8 | 4 | 108 | 93 | 3.85 |
+----------+-----+------+-----+------+
| 21.4 | 6 | 258 | 110 | 3.08 |
+----------+-----+------+-----+------+
2 changes: 1 addition & 1 deletion inst/tinytest/_tinysnapshot/docx-issue98_04.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
+----------+-----+------+-----+------+
| 21 | 6 | 160 | 110 | 3.90 |
+----------+-----+------+-----+------+
| 22.8 | 4 | 108 | 93 | 3.85 |
| 22.8 | 4 | 108 | 93 | 3.85 |
+----------+-----+------+-----+------+
| 21.4 | 6 | 258 | 110 | 3.08 |
+----------+-----+------+-----+------+
2 changes: 1 addition & 1 deletion inst/tinytest/_tinysnapshot/group_tt-3level.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
+------+-----+------+-----+------+
| 21.0 | 6 | 160 | 110 | 3.90 |
+------+-----+------+-----+------+
| 22.8 | 4 | 108 | 93 | 3.85 |
| 22.8 | 4 | 108 | 93 | 3.85 |
+------+-----+------+-----+------+
2 changes: 1 addition & 1 deletion inst/tinytest/_tinysnapshot/group_tt-3level.tex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
mpg & cyl & disp & hp & drat \\ \midrule %% TinyTableHeader
21.0 & 6 & 160 & 110 & 3.90 \\
21.0 & 6 & 160 & 110 & 3.90 \\
22.8 & 4 & 108 & 93 & 3.85 \\
22.8 & 4 & 108 & 93 & 3.85 \\
\bottomrule
\end{tblr}
\end{table}
2 changes: 1 addition & 1 deletion inst/tinytest/_tinysnapshot/group_tt-3level.typ
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ table.cell(stroke: (bottom: .05em + black), colspan: 2, align: center)[c],table.
// tinytable cell content after
[21.0], [6], [160], [110], [3.90],
[21.0], [6], [160], [110], [3.90],
[22.8], [4], [108], [ 93], [3.85],
[22.8], [4], [108], [93], [3.85],

// tinytable footer after

Expand Down
6 changes: 3 additions & 3 deletions inst/tinytest/_tinysnapshot/group_tt-html_tutorial_01.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
<td>22.8</td>
<td>4</td>
<td>108.0</td>
<td> 93</td>
<td>93</td>
<td>3.85</td>
</tr>
<tr>
Expand Down Expand Up @@ -162,14 +162,14 @@
<td>24.4</td>
<td>4</td>
<td>146.7</td>
<td> 62</td>
<td>62</td>
<td>3.69</td>
</tr>
<tr>
<td>22.8</td>
<td>4</td>
<td>140.8</td>
<td> 95</td>
<td>95</td>
<td>3.92</td>
</tr>
<tr>
Expand Down
Loading
Loading