在formattable中保留不同的行和列格式

时间:2019-06-24 03:01:06

标签: css r formattable

我正在尝试使用formattable包在R中创建一个漂亮的表,但是我遇到了一个试图对行和列进行操作的块。我想根据规则使用斜体的某些行和某些列的颜色,但是formattable()的列表输入中的最后一项优先,并擦除其他任何格式更改。

有人知道解决这个问题的方法吗?

我尝试在代码中包括对c0列的引用以进行着色,但是无法识别该引用。


library(tidyverse)
library(formattable)

df <- data.frame(c0 = c("This", "That", "This"),
                 v1 = 1:3/5, v2 = 4:6/7) %>% 
  mutate_at(vars(starts_with("v")), percent)

colouring <- function(color = "lightblue", fun = "percent", digits = 0) {
  fun = match.fun(fun)
  formatter("span", 
            x ~ fun(x, digits = digits),
            style = function(y) style(
              "background-color" = ifelse(y > 0.5, csscolor(color), csscolor("white"))
            ))
}

fonting <- formatter("span", style = x ~ style(font.style = "italic", color = "gray"))

# as is
formattable(df)

# with colour
formattable(df, list(
  area(col = 2:3) ~ colouring()))

# with italics
formattable(df, list(
  area(row = str_which(df$c0, "That")) ~ fonting))

# But using both does not preserve previous changes - - 
# colour first
formattable(df, list(
  area(col = 2:3) ~ colouring(),
  area(row = str_which(df$c0, "That")) ~ fonting
))
# italics first
formattable(df, list(
  area(row = str_which(df$c0, "That")) ~ fonting,
  area(col = 2:3) ~ colouring()
))

我希望在应用颜色规则时保留斜体。

0 个答案:

没有答案