0

我正在尝试使用 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()
))

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

4

0 回答 0