看这个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))
您产生类似这样的内容:
最终,我想按行使用color_tile
中的formattable
函数-在我自己的数据集中,我需要按行比较值,同时保持数据帧的结构。我已经尝试过转置数据框,但是这样会使我的代码和数据变得一团糟,而且我认为这种路由是不合理的。
答案 0 :(得分:0)
您可以循环提取行并对其运行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))