kableExtra formatting table 格式化科学指数

kableExtra formatting table formatting scientific exponents

我正在尝试格式化 table。我注意到一列没有我想要的格式。在下图中,您可以看到标题为“p-value”的列中的值显示为“1.0x10^-5”。我似乎无法弄清楚为什么该列中的 x 是斜体。我真正希望该列看起来像这样的每个值都像这样“1.0 x 10^-5”,没有斜体 x 和“x”前后的空格。我想知道是否有人知道如何解决这个问题?我在下面添加了一个示例数据集以及我用来创建 table.

的代码

示例代码:

 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(".4^{+5.3}_{-4.2}$", 
".7^{+5.4}_{-4.3}$", ".3^{+5.4}_{-4.4}$", ".6^{+5.5}_{-4.4}$", 
".9^{+5.5}_{-4.4}$", ".9^{+5.5}_{-4.5}$", ".2^{+5.5}_{-4.5}$", 
".8^{+5.6}_{-4.6}$", ".4^{+5.7}_{-4.7}$", ".0^{+5.8}_{-4.8}$", 
".3^{+5.8}_{-4.8}$", ".2^{+5.9}_{-4.9}$", ".8^{+6.2}_{-5.3}$", 
".4^{+6.2}_{-5.3}$", ".7^{6.2}_{-5.4}$", ".0^{+6.2}_{-5.4}$", 
".3^{+6.3}_{-5.4}$", ".4^{+6.6}_{-5.9}$", ".7^{+6.6}_{-5.9}$"
), class = "factor"), p.value = structure(c(12L, 14L, 18L), .Label = c(".0 x 10^{-5}$", 
".1 x 10^{-4}$", ".2 x 10^{-5}$", ".3 x 10^{-3}$", ".4 x 10^{-5}$", 
".5 x 10^{-3}$", ".7 x 10^{-4}$", ".0 x 10^{-4}$", ".0 x 10^{-5}$", 
".9 x 10^{-4}$", ".0 x 10^{-4}$", ".2 x 10^{-7}$", ".6 x 10^{-4}$", 
".3 x 10^{-7}$", ".6  x 10^{-4}$", ".6 x 10^{-4}$", ".8 x 10^{-4}$", 
".5 x 10^{-6}$", ".2 x 10^{-4}$"), class = "factor")), row.names = c(NA, 
3L), class = "data.frame")

如果我们从美元符号中省略 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")