Skip to content

Instantly share code, notes, and snippets.

@mcanouil
Last active February 2, 2024 23:36
Show Gist options
  • Save mcanouil/a1c3aa4f64d620e915ab4817fefe60e6 to your computer and use it in GitHub Desktop.
Save mcanouil/a1c3aa4f64d620e915ab4817fefe60e6 to your computer and use it in GitHub Desktop.
Gist addressing the question to how to display an unknown number of cross-referenceable tables from Twitter/Mastodon.
<!--
# # MIT License
#
# Copyright (c) 2024 Mickaël Canouil
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
Suppose, in a #QuartoPub document, using #rStats I have a list of length G, each item in this list is a flextable - I need to use flextable as opposed to kable, to allow for output to docx.
G is unknown apriori, but will be at least 1.
Is there any way to create a sequence of tables with associated labels and captions that could be worked out dynamically/parsed?
Manually, this can be done really easily (creating new labels etc) and knowing when to stop, but is there a way of automating this?
Source: https://mastodon.ie/@DToher/109915736491084825
-->
---
format: html
---
```{r}
#| include: false
library(palmerpenguins)
library(gt)
library(dplyr)
```
Below are tables of the first six rows of the Palmer penguins dataset by species:
```{r}
#| output: asis
#| echo: false
cat(sprintf("- `%s` (@tbl-%s)", levels(penguins[["species"]]), levels(penguins[["species"]])), sep = "\n")
```
```{r}
#| echo: false
#| output: asis
for (ispecies in levels(penguins[["species"]])) {
tab <- penguins |>
filter(species %in% ispecies) |>
select(-species) |>
head() |>
gt() |>
tab_header(title = ispecies)
cat(sep = "\n", knitr::knit_child(quiet = TRUE, text = c(
"```{r}",
"#| echo: false",
sprintf("#| tbl-cap: %s", ispecies),
sprintf("#| label: tbl-%s", ispecies),
"tab",
"```"
)))
}
```
@dtoher
Copy link

dtoher commented Feb 24, 2023

Perfect, works for flextable as well as gt, so also can be compiled to word (for the cases when I really need multiple formats of outputs).

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