KableExtra 在具有长非空白字符串的列中添加换行符

KableExtra adding linebreaks in columns with long nonwhitespace strings

我有一个长字符串,我想要 table,例如字母 A (AAA...) 的 100 倍。如果它们不适合 table,我希望 kable 将这个字符串分成多行,而不是让这些字符串溢出,如图所示 here.

我注意到 kable 实际上能够这样做,前提是您的字符串中有换行符或 -,例如参见 [​​=13=].

但是,我希望 kable 对选定的字符或任何字符进行拆分,因此输出结果将是 this,但我不知道如何实现。我查看了 SO 和 kableextra 文档,但没有运气。有什么建议吗?

下面是一个可以尝试的块。

---
title: 'rasstasrt'
sansfont: Calibri Light
output: pdf_document
---

```{r setup, include=FALSE}
library(kableExtra);
library(dplyr)
knitr::opts_chunk$set(cache = F)
```


```{r}
dt <-tibble(Items =c("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaAAa", "Item 2", "Item 3"),
            Tmd5ext_1 =c("Lorem ipsum  "),
            Text_2 =c("Duis pos "))

kableExtra::kable(dt, "latex", booktabs = F, col.names =c("Item", "Short Title", "Veryong Title"))  %>% column_spec(1:3, width = "5cm", )
```

您可以利用 Latex 中的换行符,\kableExtra::kable() 中的参数 escape = FALSE。设置 escape = FALSE 会导致 \ 被读取为换行符而不是文字 \请注意,因为 \ 是 R 中的转义字符,您需要两个斜杠 \ 以便 R 解释单个斜杠 \,因此要获得 2 个文字斜杠\ 你需要在字符串中输入 4 \\.

---
title: 'rasstasrt'
sansfont: Calibri Light
output: pdf_document
---

```{r setup, include=FALSE}
library(kableExtra)
library(dplyr)
knitr::opts_chunk$set(cache = F)
```


```{r}

dt <-tibble(Items = c("AAAAAAAAAAAAAAA\\AAAAAAAAAA\\AAAAAAAAAAA\\AAAAAAAAAAAAA\\AAAAAAaaaaAAa", 
                      "Item 2", 
                      "Item 3"),
            Tmd5ext_1 = c("Lorem ipsum  "),
            Text_2 = c("Duis pos "))

kableExtra::kable(dt, 
                  "latex", 
                  booktabs = F, 
                  col.names =c("Item", "Short Title", "Veryong Title"), 
                  escape = FALSE)  %>% 
  column_spec(1:3, width = "5cm")
```

或者,如果您想在特定数量的字符后插入换行符,您可以编写一个函数来实现。

---
title: 'rasstasrt'
sansfont: Calibri Light
output: pdf_document
---

```{r setup, include=FALSE}
library(kableExtra)
library(dplyr)
knitr::opts_chunk$set(cache = F)
```


```{r}
add_return <- function(x, len) {
  # intialize empty vector
  y <- c()

  # start at beginning of string
  i <- 1

  # Break string up into lengths of len
  while(i < nchar(x)) {

    y <- c(y,substr(x, i, i + len - 1))

    i <- i + len

  }

  # concatenate the substrings together with the newline characters
  paste0(y, collapse = "\\") 

}

dt <-
  tibble(
    Items = c(
      add_return(
        "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaAAa",
       len = 5
      ),
      "Item 2",
      "Item 3"
    ),
    Tmd5ext_1 = c("Lorem ipsum  "),
    Text_2 = c("Duis pos ")
  )

kableExtra::kable(
  dt,
  "latex",
  booktabs = F,
  col.names = c("Item", "Short Title", "Veryong Title"),
  escape = FALSE
)  %>% column_spec(1:3, width = "5cm")
```

您也可以使用正则表达式在特定字符串后插入return。

---
title: 'rasstasrt'
sansfont: Calibri Light
output: pdf_document
---

```{r setup, include=FALSE}
library(kableExtra)
library(dplyr)
knitr::opts_chunk$set(cache = F)
```


```{r}
regex_add_return <- function(x, after) {

  gsub(pattern = paste0("(",after,")"), replacement = paste0("\1\\\\"),x)

}

dt <-
  tibble(
    Items = c(
      regex_add_return(
        "AAAAAAAAAAAAAAAz123AAAAAAAAAAAAAAAz123AAAAAAAAAAz123AAAAAAAAAAAAAAAaaaaAAa",
        after = "z123"
      ),
      "Item 2",
      "Item 3"
    ),
    Tmd5ext_1 = c("Lorem ipsum  "),
    Text_2 = c("Duis pos ")
  )

kableExtra::kable(
  dt,
  "latex",
  booktabs = F,
  col.names = c("Item", "Short Title", "Veryong Title"),
  escape = FALSE
)  %>% column_spec(1:3, width = "5cm")
```

注意gsub()中有8个斜杠,因为斜杠也是正则表达式的转义字符,所以每个字面斜杠都要用斜杠转义,但是每个斜杠被用作正则表达式的转义字符也必须为 R 再次转义,需要另一个斜杠。