如何使用formattable / kableextra修改表格行的样式

时间:2018-04-20 10:16:06

标签: r dplyr knitr kableextra formattable

我有这样的吧台(what I have)。 我的问题是我希望最后一行没有颜色条(默认情况下:只有数字)

image of the code

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

提前谢谢

1 个答案:

答案 0 :(得分:0)

调用#If VBA7 Then Declare PtrSafe Function GetSystemMetrics32 Lib "user32" Alias "GetSystemMetrics" _ (ByVal nIndex As Long) As Long #Else Declare Function GetSystemMetrics32 Lib "user32" Alias "GetSystemMetrics" _ (ByVal nIndex As Long) As Long #End If Public Const SM_CXSCREEN = 0 Public Const SM_CYSCREEN = 1 Private Sub setMonitors() celTotalMonitors = GetSystemMetrics32(80) End Sub Private Sub setResolution() 'The width of the virtual screen, in pixels celScreenResolutionX = Format(GetSystemMetrics32(78), "#,##0") 'The height of the virtual screen, in pixels celScreenResolutionY = Format(GetSystemMetrics32(79), "#,##0") 'celScreenResolutionY = celScreenResolutionY.Value \ celTotalMonitors End Sub 提供了一个函数,该函数使用背景中的条形格式化矢量。因此,如果您只想格式化第一部分,请编写您自己的函数,该函数仅格式化第一个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)
  }
}