R - kableExtra - 无法在包含字符串的单元格中以 html 格式插入换行符

R - kableExtra - Trouble inserting linebreak in html format in a cell containing string

我想在文本单元格中插入换行符,但不能。在下面的示例中,我想在字符串 group 1.1group1.2

之间插入一个换行符

我尝试阅读文档("Best Practice for newline in LaTeX table")但无法解决问题

代码如下:

library(dplyr)
library(knitr)
library(kableExtra)

mydf <- data.frame(
  # group = rep(letters[1:4], each = 2),
  row = c(1:8),
  group = c("group 1.1 \n group1.2", "group 2", "group 3", "group 4", "group 5", "group 6", "group 7", "group 8")
)

mydf %>%
# mutate_all(linebreak) %>%
kable() %>%
   kable_styling()

如果我插入mutate_all(linebreak) %>%也不能解决问题

pdf

我能够通过设置 kable(escape = FALSE):

让它为 pdf 工作
library(dplyr)
library(knitr)
library(kableExtra)

mydf <- data.frame(
  # group = rep(letters[1:4], each = 2),
  row = c(1:8),
  group = c("group 1.1\ngroup1.2", "group 2", "group 3", "group 4", "group 5", "group 6", "group 7", "group 8")
)

mydf %>%
  mutate_all(linebreak) %>%
  kable("latex", escape = FALSE) %>%
  kable_styling()

html

mydf <- data.frame(
  # group = rep(letters[1:4], each = 2),
  row = c(1:8),
  group = c("group 1.1<br>group1.2", "group 2", "group 3", "group 4", "group 5", "group 6", "group 7", "group 8")
)

mydf %>%
  kable("html", escape = FALSE) %>%
  kable_styling()

参见:Printing linebreaks in HTML kable table