我正在模拟与Kable的WhatsApp对话。因为我为这两个聊天器中的每一个都有一列,所以使用长文本包装来适应单元格。我希望文本溢出到对方的聊天专栏,以更好地模拟对话。
chat <- tibble(
john = c("looooooooooong sentence1", NA, "looooooooooong sentence2", NA),
jane = c(NA, "even looooooooooongerrrrrrrrrr sentence1", NA, "even looooooooooongerrrrrr sentence2"))
chat %>%
knitr::kable( format = "html",
table.attr = "style='width:50%;'",
align = "lr",
col.names = NULL) %>%
row_spec(which(!is.na(chat$jane)), background = "#DCF8C6")我的产出:

我想要的:

我还设置了options(knitr.kable.NA = '')以隐藏NA的
发布于 2022-11-20 23:20:06
这里有一种可能的方法,使用align参数row_spec来控制文本。
library(tibble)
library(knitr)
library(kableExtra)
library(tidyr)
library(dplyr)
# convert table into long format
chat <-
chat |>
pivot_longer(everything()) |>
na.omit()
chat |>
select(value) |>
kable( format = "html",
table.attr = "style='width:50%;'",
col.names = NULL) |>
column_spec(1, width = "100mm") |>
row_spec(which(chat$name == "jane"), background = "#DCF8C6", align = "r")

https://stackoverflow.com/questions/74512624
复制相似问题