Created
March 28, 2023 15:23
-
-
Save yjunechoe/711d6a54c51acefd690c81eec47588c0 to your computer and use it in GitHub Desktop.
Incremental presentation of {gt} html table rows in Quarto slides
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "incremental table" | |
format: revealjs | |
editor: visual | |
--- | |
## Make `{gt}` table | |
```{css} | |
table.gt_table { | |
font-size: .5em; | |
} | |
``` | |
```{r} | |
#| echo: true | |
library(gt) | |
tab_1 <- gt(exibble, rowname_col = "row", groupname_col = "group") | |
tab_1 | |
``` | |
## Get rows elements | |
```{r} | |
#| echo: true | |
library(xml2) | |
xml_gt <- gt::as_raw_html(tab_1) |> read_html() | |
rows_xpath <- ".//tr[.//th[contains(@class, 'gt_row')]]" | |
tab1_rows <- xml_gt |> xml_find_all(rows_xpath) | |
tab1_rows | |
``` | |
## Manipulate row attrs | |
```{r} | |
#| echo: true | |
tab1_row_attrs <- xml_attrs(tab1_rows) | |
tab1_new_row_attrs <- lapply(seq_along(tab1_rows), function(i) { | |
row_attr <- tab1_row_attrs[[i]] | |
row_attr["fragment-index"] <- i | |
row_attr["class"] <- paste(na.omit(c(row_attr["class"], "fragment")), collapse = " ") | |
row_attr | |
}) | |
xml_set_attrs(tab1_rows, tab1_new_row_attrs) | |
tab1_rows | |
``` | |
## Final product | |
```{r} | |
#| echo: true | |
tab1_incremental <- xml_gt |> | |
xml_find_first(".//div") |> | |
as.character() |> | |
htmltools::HTML() | |
tab1_incremental | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
20230328_111940.mp4