如果列超过 R Markdown 中的页面容量,如何从 table 拆分行?

How to split row from table if column exceeds the page capacity in R Markdown?

下面我有一个脚本,其中包含第 5 个文本列,该列的内容太多,超出了页面的大小。虽然我添加了 longtable = Tlatex_options= "repeat_header" 它只会在多个页面中继续 table 但是如果该行超过页面它会被切断。我怎样才能让 table 在不丢失文本的情况下继续移动。

df %>%
kableExtra::kbl(.,booktabs = T,longtable = T)%>%    
row_spec(0,background = "#F6F6F6",color="black")%>%
kable_styling(bootstrap_options = "striped", font_size = 9,latex_options = 
c("hold_position","repeat_header"),position = "left") %>%
column_spec(1,width = "2.0cm") %>%
column_spec(2,width = "2.5cm") %>%
column_spec(3,width = "2.5cm") %>%
column_spec(4,width = "4.5cm")%>%
column_spec(5,width="10.0cm")

这里有一个解决方法,可以拆分包含长文本的单元格。它的工作原理是根据字数将文本分成两块,因此可以通过反复试验轻松调整。

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = FALSE)

library(kableExtra)
library(wakefield) # for generating long text
library(dplyr)
library(tidyr)
library(stringr)

```
```{r df, include=FALSE}

set.seed(123)

#sample dataset

df <- data.frame(a = 1:6,
                 b = month.name[1:6],
                 c = names(mtcars)[1:6],
                 d = names(islands)[1:6],
                 e = c(paragraph(2), paste(paragraph(6), collapse = "; "), paragraph(3)))

#create new data frame, cells with long text split into to
df_new <- 
  df %>% 
  mutate(f = ifelse(str_length(e)>2000, word(e, 301, -1), NA_character_),
         e = ifelse(str_length(e)>2000, word(e, 1, 300), e)) %>% 
  pivot_longer(cols = c(f, e), values_to = "e") %>% 
  na.omit() %>% 
  arrange(a, name) %>% 
  select(-name)

```
```{r long-table, results='asis'}

df_new %>%
kbl(booktabs = TRUE, 
    longtable = TRUE) %>%
  row_spec(0, background = "#F6F6F6", color = "black") %>%
  landscape() %>%
  kable_styling(bootstrap_options = "striped",
              font_size = 9,
              latex_options = c("hold_position","repeat_header"),position = "left") %>%
  column_spec(1, width = "2.0cm") %>%
  column_spec(2, width = "2.5cm") %>%
  column_spec(3, width = "2.5cm") %>%
  column_spec(4, width = "4.5cm") %>%
  column_spec(5, width = "10.0cm")

```