0

我有这样的酒吧桌(我有什么)。我的问题是我希望最后一行没有颜色条(默认情况下:只有数字)

代码图像

data(mtcars)

library(knitr)
library(kableExtra)
library(formattable)
library(dplyr)
library(stringr)

mtcars[1:5, 1:4] %>%
  mutate(
    car = row.names(.),
    mpg = color_bar("lightgreen")(mpg),
    cyl = color_bar("lightgreen")(cyl),
    disp = color_bar("lightgreen")(disp),
    hp = color_bar("lightgreen")(hp)
  ) %>%
  select(car, everything()) %>%
  kable("html", escape = F) %>%
  kable_styling("hover", full_width = F) %>%
  column_spec(5, width = "3cm") 

先感谢您

4

1 回答 1

0

该调用color_bar("lightgreen")提供了一个函数,该函数在背景中格式化带有条形的向量。因此,如果您只想格式化第一部分,请编写您自己的函数,仅以n-1这种方式格式化第一个条目。例如:

my_color_bar <- function(color) {
  mainpart <- color_bar(color)
  function(x) {
    start <- x[-length(x)]
    last <- x[length(x)]          
    c(mainpart(start), last)
  }
}

然后使用该函数而不是原始函数生成表:

mtcars[1:5, 1:4] %>%
        mutate(
                car = row.names(.),
                mpg = my_color_bar("lightgreen")(mpg),
                cyl = my_color_bar("lightgreen")(cyl),
                disp = my_color_bar("lightgreen")(disp),
                hp = my_color_bar("lightgreen")(hp)
        ) %>%
        select(car, everything()) %>%
        kable("html", escape = FALSE) %>%
        kable_styling("hover", full_width = FALSE) %>%
        column_spec(5, width = "3cm") 
于 2018-04-20T22:22:08.117 回答