Skip to content

Commit

Permalink
group_tt row label vector
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Sep 22, 2024
1 parent 2815210 commit cc30dba
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 11 deletions.
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 is concise. 'HTML' tables can be customized using the flexible 'Bootstrap' framework, and 'LaTeX' code with the 'tabularray' package.
Version: 0.4.0.5
Version: 0.4.0.6
Imports:
methods
Depends:
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

New:

* `group_tt(i = vec)` accepts a vector of labels of length equal to the number of rows in the dataset.
* `tt()` gets an `escape` argument. Thanks to Cameron Patrick for the feature request.
* The `i` argument in `style_tt()` now accepts a logical matrix of same dimensions as `x`, to style specific cells, rather than all combinations of `i` and `j` vectors. Thanks to @dhicks for the feature request #329.
* `style_tt()` gets new `output` argument for conditional styling based on output format.
Expand Down
24 changes: 22 additions & 2 deletions R/group_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @export
#' @inheritParams tt
#' @inheritParams style_tt
#' @param i A named list of row indices to group. The names of the list will be used as labels. The indices represent the position where labels should be inserted in the original table. For example,
#' @param i A vector of labels with length equal to the number of rows in `x`, or a named list of row indices to group. The names of the list will be used as labels. The indices represent the position where labels should be inserted in the original table. For example,
#' + `i=list("Hello"=5)`: insert the "Hello" label after the 4th row in the original table.
#' + `i=list("Hello"=2, "World"=2)`: insert the two labels consecutively after the 1st row in the original table.
#' + `i=list("Foo Bar"=0)`: insert the label in the first row after the header.
Expand All @@ -15,6 +15,14 @@
#' Warning: The `style_tt()` can normally be used to style the group headers, as expected, but that feature is not available for Markdown and Word tables.
#' @examples
#'
#' # vector of row labels
#' dat <- data.frame(
#' label = c("a", "a", "a", "b", "b", "c", "a", "a"),
#' x1 = rnorm(8),
#' x2 = rnorm(8))
#' tt(dat[, 2:3]) |> group_tt(i = dat$label)
#'
#' # named lists of labels
#' tt(mtcars[1:10, 1:5]) |>
#' group_tt(
#' i = list(
Expand Down Expand Up @@ -43,13 +51,19 @@
#' group_tt(j = list("Hello" = 1:2, "World" = 3:4, "Hello" = 5:6)) |>
#' group_tt(j = list("Foo" = 1:3, "Bar" = 4:6))
#'

group_tt <- function(x, i = NULL, j = NULL, indent = 1, ...) {
# ... is important for ihead passing

if (!inherits(x, "tinytable")) stop("`x` must be generated by `tinytable::tt()`.", call. = FALSE)
if (is.null(i) && is.null(j)) stop("At least one of `i` or `j` must be specified.", call. = FALSE)
assert_integerish(indent, lower = 0)

# vector of labels
if (isTRUE(check_atomic_vector(i))) {
i <- sanitize_group_vec2list(i)
}

i <- sanitize_group_index(i, hi = nrow(x) + 1, orientation = "row")
j <- sanitize_group_index(j, hi = ncol(x), orientation = "column")

Expand All @@ -66,7 +80,13 @@ group_tt <- function(x, i = NULL, j = NULL, indent = 1, ...) {
return(x)
}


sanitize_group_vec2list <- function(vec) {
rle_result <- rle(vec)
idx <- cumsum(c(1, utils::head(rle_result$lengths, -1)))
idx <- as.list(idx)
names(idx) <- rle_result$values
return(idx)
}

sanitize_group_index <- function(idx, hi, orientation) {
if (is.null(idx)) {
Expand Down
27 changes: 27 additions & 0 deletions inst/tinytest/_tinysnapshot/group_tt-vector_row_labels.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
+-------------+------------+
| x1 | x2 |
+=============+============+
| a |
+-------------+------------+
| -0.65470589 | -0.1692089 |
+-------------+------------+
| -0.49824606 | 0.5763153 |
+-------------+------------+
| 0.33230864 | -0.5149627 |
+-------------+------------+
| b |
+-------------+------------+
| 1.12146881 | -0.4805023 |
+-------------+------------+
| -0.06394603 | 0.7634626 |
+-------------+------------+
| c |
+-------------+------------+
| 1.53760239 | 0.7914405 |
+-------------+------------+
| a |
+-------------+------------+
| -0.11144127 | 1.7435388 |
+-------------+------------+
| 0.24231271 | 1.6937668 |
+-------------+------------+
9 changes: 9 additions & 0 deletions inst/tinytest/test-group_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ source("helpers.R")
using("tinysnapshot")


# vector row labels
set.seed(48103)
dat <- data.frame(
label = c("a", "a", "a", "b", "b", "c", "a", "a"),
x1 = rnorm(8),
x2 = rnorm(8))
tab <- tt(dat[, 2:3]) |> group_tt(i = dat$label)
expect_snapshot_print(tab, label = "group_tt-vector_row_labels.md")

# 3 level: markdown
options(tinytable_print_output = "markdown")
x <- mtcars[1:3, 1:5]
Expand Down
10 changes: 9 additions & 1 deletion man/group_tt.Rd

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

26 changes: 19 additions & 7 deletions vignettes/tinytable.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,6 @@ nrow(a)
colnames(a)
```

Rename columns:

```{r}
colnames(a) <- c("a", "b")
a
```

Tables can be combined with the usual `rbind()` function:

```{r}
Expand All @@ -318,3 +311,22 @@ Bind tables by position rather than column names:
rbind2(a, b, use_names = FALSE)
```


## Renaming columns

As noted above, `tinytable` tries to be standards-compliant, by defining methods for many base `R` functions. The benefit of this approach is that instead of having to learn a `tinytable`-specific syntax, users can rename columns using all the tools they already know:

```{r}
a <- tt(mtcars[1:2, 1:2])
colnames(a) <- c("a", "b")
a
```

In a pipe-based workflow, we can use the `setNames()` function from base `R`:

```{r}
mtcars[1:2, 1:2] |>
tt() |>
setNames(c("a", "b"))
```

0 comments on commit cc30dba

Please sign in to comment.