可以将 formattable 与 kableextra row wise 结合起来吗?

Possible to combine formattable with kableextra row wise?

看看这个 relevant 插图:

使用代码:

library(tidyverse)
library(knitr)
library(kableExtra)
library(formattable)

set.seed(1)
data.frame(letters = letters[1:5],
           foo = rnorm(5, 20),
           bar = rnorm(5, 20),
           baz = rnorm(5, 20),
           bash = rnorm(5, 20)) %>%
    mutate(foo = color_tile("pink", "lightblue")(foo)) %>%
    kable(escape = F) %>%
    kable_styling("hover", full_width = F) %>%
    column_spec(5, width = "3cm") %>%
    add_header_above(c(" ", "Hello" = 2, "World" = 2))

你产生了这样的东西:

最终我想使用 formattable rowwise 中的 color_tile 函数 - 在我自己的数据集中,我需要按行比较值,同时保持数据框的结构。我试过转置数据框,但它弄乱了我的代码和数据,我认为这条路线不合理。

您可以在循环中提取行,然后 运行 color_tile() 在它们上面。

library(tidyverse)
library(knitr)
library(kableExtra)
library(formattable)

set.seed(1)
df <- data.frame(letters = letters[1:5],
           foo = rnorm(5, 20),
           bar = rnorm(5, 20),
           baz = rnorm(5, 20),
           bash = rnorm(5, 20),
           stringsAsFactors = FALSE)

for(i in 1:nrow(df)) df[i,] <- color_tile("pink", "lightblue")(df[i,])

df %>%
  kable(escape = F) %>%
  kable_styling("hover", full_width = F) %>%
  column_spec(5, width = "3cm") %>%
  add_header_above(c(" ", "Hello" = 2, "World" = 2))