我正试着格式化一张桌子。我注意到有一列没有我想要的格式。在下面的图片中,您可以看到标题为"p-value“的列中的值显示为"1.0_x_10^-5”。我似乎不明白为什么那列中的x是斜体。我真正想让列看起来像这样的是,每个值都是"1.0 x 10^-5“,没有斜体x,在"x”前后没有空格。我想知道有没有人知道怎么解决这个问题?我在下面添加了一个示例数据集以及用于创建表的代码。

示例代码:
kbl(df.test, format = "html", table.attr = "style='width:40%;'", escape = F, align = "r") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
kable_classic(full_width = F, html_font = "Times New Roman")示例数据:
structure(list(SNP = c("abc", "abc", "abc"), Gene = c("xyz",
"xyz", "xyz"), Cases = c(87L, 86L, 72L), `% Population` = structure(19:17, .Label = c("$12.4^{+5.3}_{-4.2}$",
"$12.7^{+5.4}_{-4.3}$", "$13.3^{+5.4}_{-4.4}$", "$13.6^{+5.5}_{-4.4}$",
"$13.9^{+5.5}_{-4.4}$", "$13.9^{+5.5}_{-4.5}$", "$14.2^{+5.5}_{-4.5}$",
"$14.8^{+5.6}_{-4.6}$", "$15.4^{+5.7}_{-4.7}$", "$16.0^{+5.8}_{-4.8}$",
"$16.3^{+5.8}_{-4.8}$", "$17.2^{+5.9}_{-4.9}$", "$19.8^{+6.2}_{-5.3}$",
"$20.4^{+6.2}_{-5.3}$", "$20.7^{6.2}_{-5.4}$", "$21.0^{+6.2}_{-5.4}$",
"$21.3^{+6.3}_{-5.4}$", "$25.4^{+6.6}_{-5.9}$", "$25.7^{+6.6}_{-5.9}$"
), class = "factor"), p.value = structure(c(12L, 14L, 18L), .Label = c("$1.0 x 10^{-5}$",
"$1.1 x 10^{-4}$", "$1.2 x 10^{-5}$", "$1.3 x 10^{-3}$", "$1.4 x 10^{-5}$",
"$1.5 x 10^{-3}$", "$1.7 x 10^{-4}$", "$2.0 x 10^{-4}$", "$2.0 x 10^{-5}$",
"$2.9 x 10^{-4}$", "$4.0 x 10^{-4}$", "$5.2 x 10^{-7}$", "$5.6 x 10^{-4}$",
"$6.3 x 10^{-7}$", "$6.6 x 10^{-4}$", "$6.6 x 10^{-4}$", "$7.8 x 10^{-4}$",
"$8.5 x 10^{-6}$", "$9.2 x 10^{-4}$"), class = "factor")), row.names = c(NA,
3L), class = "data.frame")发布于 2021-10-12 15:25:36
如果我们忽略了美元符号中的x,它将不会被格式化为一个等式。
library(dplyr)
library(kableExtra))
data %>% mutate(p.value = gsub(x = p.value, pattern = " x ", replacement = "$ x $")) %>% # Replace "x" with "$ x $"
kbl(format = "html", table.attr = "style='width:40%;'", escape = F, align = "r") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
kable_classic(full_width = F, html_font = "Times New Roman")

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