0

看看这个相关的小插曲:

使用代码:

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))

你产生这样的东西:

在此处输入图像描述

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

4

1 回答 1

1

您可以在循环中提取行并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))
于 2019-01-16T16:18:47.693 回答