Skip to content

Commit

Permalink
issue #133: markdown group_tt works with i style_tt indices
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Feb 10, 2024
1 parent 27bd21f commit 9dc97dd
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
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.9008
Version: 0.0.3.9009
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 @@ -13,6 +13,7 @@ New:
Bugfix:

- Markdown group columns when labels are wider than columns. Thanks to @etiennebacher for report #127.
- Markdown group rows broke indexing when using `style_tt()`. Thanks to @strengejacke for report #133.

## 0.0.3

Expand Down
14 changes: 14 additions & 0 deletions R/style_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ style_grid <- function(x,
ival <- if (is.null(i)) seq_len(meta(x, "nrows")) else i
jval <- if (is.null(j)) seq_len(meta(x, "ncols")) else j

# Unlike other formats, Markdown inserts `group_tt()` row labels after styling. This aligns the `i` index to the full columns.
gr <- meta(x, "lazy_group")
gr <- Filter(function(k) !is.null(k$i), gr)
# do not style spanning row labels
lab_idx <- drop(unlist(lapply(gr, function(k) k$i)))
lab_idx <- lab_idx + cumsum(rep(1, length(lab_idx))) - 1
ival <- setdiff(ival, lab_idx)
for (g in gr) {
for (lab in g$i) {
ival[ival > lab - 1] <- ival[ival > lab - 1] - 1
}
lab_idx <- c(lab_idx, g$i)
}

for (col in seq_along(out)) {
out[[col]] <- as.character(out[[col]])
}
Expand Down
26 changes: 26 additions & 0 deletions inst/tinytest/_tinysnapshot/markdown-group_i_style_tt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

+----------+-------+---------+
| Hello | World |
+----------+-------+---------+
| mpg | cyl | disp |
+==========+=======+=========+
| 21 | 6 | 160 |
+----------+-------+---------+
| *21* | *6* | *160* |
+----------+-------+---------+
| Hello |
+----------+-------+---------+
| *22.8* | *4* | *108* |
+----------+-------+---------+
| 21.4 | 6 | 258 |
+----------+-------+---------+
| ~~18.7~~ | ~~8~~ | ~~360~~ |
+----------+-------+---------+
| World |
+----------+-------+---------+
| **18.1** | **6** | **225** |
+----------+-------+---------+
| 14.3 | 8 | 360 |
+----------+-------+---------+
| 24.4 | 4 | 146.7 |
+----------+-------+---------+
11 changes: 11 additions & 0 deletions inst/tinytest/test-markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,16 @@ tab <- tt(test) |>
expect_snapshot_print(tab, label = "markdown-group_j_wider_2")


# Issue #133
tab <- tt(mtcars[1:8, 1:3]) |>
group_tt(i = list("Hello" = 3, "World" = 6)) |>
group_tt(j = list("Hello" = 1, "World" = 2:3)) |>
style_tt(i = 2:4, italic = TRUE) |>
style_tt(i = 6, strikeout = TRUE) |>
style_tt(i = 8, bold = TRUE)
expect_snapshot_print(tab, label = "markdown-group_i_style_tt")




options(tinytable_print_output = NULL)

0 comments on commit 9dc97dd

Please sign in to comment.