[英]How do I automatically split huxtables across multiple pages?
目標:
在我正在開發的 Shiny 應用程序中,用戶可以上傳一個.csv
文件,該文件被轉換為 huxtable、格式化並最終可以下載為.pdf
。 由於用戶可以上傳自己的數據集,我事先不知道它將包含多少行,也不知道該表是否需要跨多個頁面拆分。 因此,如果表格在.pdf
文件中超過一頁時自動拆分,將會很方便。 我知道我可以使用flextable
等其他包來實現這一點,但flextable
在渲染到 pdf 時不支持填充。
問題:
如果表格變得太長,它將被推到下一頁,並且不會顯示任何超過該頁面的內容。
我知道 function split_across()
,但我無法用它解決我的問題。
如果我設置一個固定值,例如在每第二十行之后進行拆分,然后一個單元格包含大量文本並且文本被換行並因此占用更多空間,我再次遇到同樣的問題。
代表:
---
output:
pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(huxtable)
```
# Title Text
```{r}
faithful[1, 1] <- paste0(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod ",
"tempor incididunt ut, labore et dolore magna aliqua."
)
as_hux(faithful) |>
set_wrap(everywhere, everywhere, TRUE) |>
set_width(0.8)
```
您對split_across
的想法實際上是正確的方法。 你需要:
split_across
將您的表格分成多個。huxtables
的list
(!)。to_latex
來渲染它們。LaTeX
包。---
output:
pdf_document:
extra_dependencies: ["array","adjustbox","threeparttable","tabularx","colortbl","hhline","calc"]
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(huxtable)
library(magrittr)
library(purrr)
```
# Title Text
```{r, results = "asis"}
faithful[1, 1] <- paste0(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod ",
"tempor incididunt ut, labore et dolore magna aliqua."
)
## just to see if all rows are added
faithful <- cbind(row_nr = 1:nrow(faithful), faithful)
rows_first_page <- 20L
max_rows_per_page <- 23L
hux_list <- as_hux(faithful) %>%
set_wrap(everywhere, everywhere, TRUE) %>%
set_width(0.8) %>%
split_across(., after = seq(rows_first_page , nrow(.), by = max_rows_per_page))
hux_lat <- imap_chr(hux_list,
~ .x %>%
set_label(
paste0("tab:",
knitr::opts_current$get("label"), "-", .y)) %>%
to_latex()) %>%
paste(collapse = "\n\n")
cat(hux_lat)
```
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.