gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:46:12 25 4
gpt4 key购买 nike

我有一个很长的字符串,我想将其放入表格中,例如 100x 字母 A (AAA...)。如果这些字符串不适合表,我希望 kable 将其拆分为多行,而不是使这些字符串溢出,如图所示 here .

我注意到 kable 实际上能够这样做,前提是字符串中有换行符或 -,例如参见 here .

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

下面是一个可以玩的 block 。

---
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")
```

enter image description here

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

---
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")
```

enter image description here

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

---
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")
```

enter image description here

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

关于r - KableExtra 在具有长非空白字符串的列中添加换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61385461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com