[英]Is there a way to export multiple gt tables to a single HTML output
I have a list of many (can be dozens of) tables created with the gt
package in R.我有一个使用 R 中的
gt
package 创建的许多(可能是几十个)表的列表。 I would like to export them all as a single HTML file with a little space between each table.我想将它们全部导出为单个 HTML 文件,每个表之间有一点空间。 Currently, I have been exporting each table individually, reading them into an RMarkdown with the
xfun
package, and knitting to a single HTML file.目前,我一直在单独导出每个表,使用 xfun package 将它们读入
xfun
,并编织成单个 HTML 文件。 I'm curious if there is a method to cut out the intermediate step of saving each table individually and reading into the Rmd.我很好奇是否有一种方法可以省去单独保存每个表并读入 Rmd 的中间步骤。
Example:例子:
library(gt)
library(tidyverse)
tbl_list <- list(mtcar_tbl = mtcars %>% gt(),
iris_tbl = iris %>% gt(),
cars_tbl = cars %>% gt())
purrr::map(seq_along(tbl_list), function(rownum){
htmltools::save_html(html = tbl_list[[rownum]],
file = paste0("test",rownum,".html"))
})
RMarkdown to combine and export tables: RMarkdown 合并和导出表格:
---
title: ""
output: html_document
---
```{r, echo=FALSE,message=FALSE,warning=FALSE}
library(xfun)
```
```{r echo=FALSE}
htmltools::includeHTML("test1.html")
```
<br><br>
```{r echo=FALSE}
htmltools::includeHTML("test2.html")
```
<br><br>
```{r echo=FALSE}
htmltools::includeHTML("test3.html")
```
One option would be to use purrr::walk
with chunk option results="asis"
to directly print your tables without the intermediate step.一种选择是使用带有块选项
results="asis"
的purrr::walk
直接打印您的表格,而无需中间步骤。 To add the line breaks after each table use cat("<br><br>")
:要在每个表后添加换行符,请使用
cat("<br><br>")
:
Note: For the reprex I just print the head
of each table.注意:对于reprex,我只打印每个表的
head
。
---
title: "Untitled"
output: html_document
date: "2022-09-19"
---
```{r echo=FALSE, results='asis', warning=FALSE, message=FALSE}
library(gt)
library(tidyverse)
tbl_list <- list(mtcar_tbl = mtcars,
iris_tbl = iris,
cars_tbl = cars)
tbl_list <- purrr::map(tbl_list, ~ head(.x) %>% gt() )
purrr::walk(tbl_list, function(x) { print(x); cat("<br><br>") })
```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.