Skip to content

Commit

Permalink
issue #401 style_tt("notes", italic = TRUE)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Dec 7, 2024
1 parent 2260238 commit 84e590c
Show file tree
Hide file tree
Showing 8 changed files with 470 additions and 374 deletions.
6 changes: 3 additions & 3 deletions 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 is concise. 'HTML' tables can be customized using the flexible 'Bootstrap' framework, and 'LaTeX' code with the 'tabularray' package.
Version: 0.6.1.1
Version: 0.6.1.2
Imports:
methods
Depends:
Expand Down Expand Up @@ -31,8 +31,8 @@ Suggests:
URL: https://vincentarelbundock.github.io/tinytable/
BugReports: https://github.com/vincentarelbundock/tinytable/issues
Authors@R: c(
person("Vincent", "Arel-Bundock",
email = "[email protected]",
person("Vincent", "Arel-Bundock",
email = "[email protected]",
role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2042-7063")))
License: GPL (>= 3)
Expand Down
14 changes: 9 additions & 5 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Bugs:

* `save_tt("file.pdf")` works with colors. Thanks to @olivedv for the report and solution #395.

New:

* `style_tt("notes")` can style footnotes.

Misc:

* Documentation improvements
Expand Down Expand Up @@ -92,7 +96,7 @@ MathJax = {
* `format_tt(num_big_mark)` applies to integer columns.
* Use `getOption("viewer")` instead of `rstudioapi::viewer()` for positron support
* `glue::glue()` string is accepted by `format_tt()`. Thanks to @LukasWallrich for report #792 on the `modelsummary` repository.
* Support Github Flavored Markdown (`gfm`) output. Thanks to @kylebutts for contribution #315.
* Support Github Flavored Markdown (`gfm`) output. Thanks to @kylebutts for contribution #315.
* `theme_tt("rotate")` to rotate tables in LaTeX or Typst.
* `save_tt("/path/to/file")` returns the file path invisibly. Thanks to @yjunechoe for issue #328.

Expand Down Expand Up @@ -197,7 +201,7 @@ New features:

* `rbind()` and `rbind2()` can be used to stack `tinytable` objects. `rbind2()` is more flexible than `rbind()`. See `?tinytable::rbind2`
* New output format in `print()`: "dataframe"
* Rename table headers: `colnames(tab) <- c("a", "b", "c")`
* Rename table headers: `colnames(tab) <- c("a", "b", "c")`
* `theme_tt("resize")` gets a `direction` argument with "up", "down", "both" options. Thanks to @MarcoPortmann for feature request #207

Minor:
Expand All @@ -216,7 +220,7 @@ New function `theme_tt()`:

* Function to apply collections of transformations to a `tinytable`.
* Visual themes:
- grid, void, striped, bootstrap, default
- grid, void, striped, bootstrap, default
* `resize`: Insert a LaTeX table in a `resizebox` environment to ensure a table fits the page, or to scale it to a fraction of `\linewidth`
* `placement`: Determine where a LaTeX table float is positioned. Ex: `[H]`, `[htbp]`
* `multipage`: Split long LaTeX tables across multiple pages with (optional) repeated headers/footers. Uses the `longtblr` environment from `tabularray`.
Expand Down Expand Up @@ -284,7 +288,7 @@ Bugfix:

New:

- `Typst` tables are now supported using the `tablex` extension:
- `Typst` tables are now supported using the `tablex` extension:
- https://typst.app/
- https://github.com/PgBiel/typst-tablex
- `escape` argument in `format_tt()` escapes or substitutes special characters in LaTeX or HTML output to prevent compilation and rendering errors.
Expand Down Expand Up @@ -315,7 +319,7 @@ Bug fixes:

Documentation:

- Improved vignette on the package website.
- Improved vignette on the package website.
- Various documentation updates.
- Math in $$ is the new recommendation.

Expand Down
189 changes: 96 additions & 93 deletions R/build_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,106 +4,109 @@
#
# THE ORDER MATTERS A LOT!
build_tt <- function(x, output = NULL) {
output <- sanitize_output(output)


x <- switch(output,
html = swap_class(x, "tinytable_bootstrap"),
latex = swap_class(x, "tinytable_tabularray"),
markdown = swap_class(x, "tinytable_grid"),
gfm = swap_class(x, "tinytable_grid"),
typst = swap_class(x, "tinytable_typst"),
dataframe = swap_class(x, "tinytable_dataframe"),
)

x@output <- output

for (th in x@lazy_theme) {
fn <- th[[1]]
args <- th[[2]]
args[["x"]] <- x
x <- do.call(fn, args)
}

tab <- x@table_dataframe

# strip ANSI from `tibble`/`pillar`; keep for markdown
if (isTRUE(check_dependency("fansi"))) {
for (col in seq_along(tab)) {
if (isTRUE(x@output == "html")) {
tab[[col]] <- as.character(fansi::to_html(tab[[col]], warn = FALSE))
} else if (isTRUE(!x@output %in% c("markdown", "dataframe"))) {
tab[[col]] <- as.character(fansi::strip_ctl(tab[[col]]))
}
output <- sanitize_output(output)


x <- switch(output,
html = swap_class(x, "tinytable_bootstrap"),
latex = swap_class(x, "tinytable_tabularray"),
markdown = swap_class(x, "tinytable_grid"),
gfm = swap_class(x, "tinytable_grid"),
typst = swap_class(x, "tinytable_typst"),
dataframe = swap_class(x, "tinytable_dataframe"),
)

x@output <- output

# apply the style_notes
x <- style_notes(x)

for (th in x@lazy_theme) {
fn <- th[[1]]
args <- th[[2]]
args[["x"]] <- x
x <- do.call(fn, args)
}
}
x@table_dataframe <- tab

# format data before drawing the table
for (l in x@lazy_format) {
l[["x"]] <- x
x <- eval(l)
}

# add footnote markers just after formatting, otherwise appending converts to string
x <- footnote_markers(x)

# plots and images
for (l in x@lazy_plot) {
l[["x"]] <- x
x <- eval(l)
}

# data frame we trim strings, pre-padded for markdown
if (x@output == "dataframe") {
tmp <- x@table_dataframe
for (i in seq_along(tmp)) {
tmp[[i]] <- trimws(tmp[[i]])

tab <- x@table_dataframe

# strip ANSI from `tibble`/`pillar`; keep for markdown
if (isTRUE(check_dependency("fansi"))) {
for (col in seq_along(tab)) {
if (isTRUE(x@output == "html")) {
tab[[col]] <- as.character(fansi::to_html(tab[[col]], warn = FALSE))
} else if (isTRUE(!x@output %in% c("markdown", "dataframe"))) {
tab[[col]] <- as.character(fansi::strip_ctl(tab[[col]]))
}
}
}
x@table_dataframe <- tmp
}

# markdown styles need to be applied before creating the table, otherwise there's annoying parsing, etc.
if (x@output %in% c("markdown", "gfm", "dataframe")) {
x <- style_eval(x)
}

# draw the table
x <- tt_eval(x)

ihead <- 0
for (idx in seq_along(x@lazy_group)) {
l <- x@lazy_group[[idx]]
l[["x"]] <- x
if (length(l[["j"]]) > 0) {
ihead <- ihead - 1
l[["ihead"]] <- ihead
x@table_dataframe <- tab

# format data before drawing the table
for (l in x@lazy_format) {
l[["x"]] <- x
x <- eval(l)
}
x <- eval(l)
}

if (!x@output %in% c("markdown", "gfm", "dataframe")) {
for (l in x@lazy_style) {
l[["x"]] <- x
# output-specific styling
if (is.null(l$output) || isTRUE(x@output == l$output)) {

# add footnote markers just after formatting, otherwise appending converts to string
x <- footnote_markers(x)

# plots and images
for (l in x@lazy_plot) {
l[["x"]] <- x
x <- eval(l)
}
}
}

# markdown styles are applied earlier
if (!x@output %in% c("markdown", "gfm", "dataframe")) {
x <- style_eval(x)
}
# data frame we trim strings, pre-padded for markdown
if (x@output == "dataframe") {
tmp <- x@table_dataframe
for (i in seq_along(tmp)) {
tmp[[i]] <- trimws(tmp[[i]])
}
x@table_dataframe <- tmp
}

x <- finalize(x)
# markdown styles need to be applied before creating the table, otherwise there's annoying parsing, etc.
if (x@output %in% c("markdown", "gfm", "dataframe")) {
x <- style_eval(x)
}

# draw the table
x <- tt_eval(x)

ihead <- 0
for (idx in seq_along(x@lazy_group)) {
l <- x@lazy_group[[idx]]
l[["x"]] <- x
if (length(l[["j"]]) > 0) {
ihead <- ihead - 1
l[["ihead"]] <- ihead
}
x <- eval(l)
}

if (!x@output %in% c("markdown", "gfm", "dataframe")) {
for (l in x@lazy_style) {
l[["x"]] <- x
# output-specific styling
if (is.null(l$output) || isTRUE(x@output == l$output)) {
x <- eval(l)
}
}
}

# markdown styles are applied earlier
if (!x@output %in% c("markdown", "gfm", "dataframe")) {
x <- style_eval(x)
}

x@table_string <- lines_drop_consecutive_empty(x@table_string)
if (output == "gfm") {
assert_dependency("pandoc")
x@table_string <- paste(pandoc::pandoc_convert(text = x@table_string, to = "gfm"), collapse = "\n")
}
x <- finalize(x)

x@table_string <- lines_drop_consecutive_empty(x@table_string)
if (output == "gfm") {
assert_dependency("pandoc")
x@table_string <- paste(pandoc::pandoc_convert(text = x@table_string, to = "gfm"), collapse = "\n")
}

return(x)
return(x)
}
Loading

0 comments on commit 84e590c

Please sign in to comment.