Skip to content

Instantly share code, notes, and snippets.

@yjunechoe
Created March 28, 2023 15:23
Show Gist options
  • Save yjunechoe/711d6a54c51acefd690c81eec47588c0 to your computer and use it in GitHub Desktop.
Save yjunechoe/711d6a54c51acefd690c81eec47588c0 to your computer and use it in GitHub Desktop.
Incremental presentation of {gt} html table rows in Quarto slides
---
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
```
@yjunechoe
Copy link
Author

20230328_111940.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment